Ex: GUI Roman Numerals

The non-GUI version of this program is on an earlier page.  The purpose of this page is to demo GUI use and structure.

# _author_ = "John Oakey"
import tkinter
from tkinter import*
from tkinter import ttk

root = tkinter.Tk()     # open top level window

# Get monitor display dimensions
H = root.winfo_screenheight()
W = root.winfo_screenwidth()
# set toplevel window to full monitor display
root.geometry(str(W) + "x" + str(H))
answer = StringVar()
answer.set("Answer will display here")
intro = StringVar()
intro.set("Enter an integer from 1 to 5000 here")
begin = StringVar()
begin.set('BEGIN')
# ______________________________________________________________
# "go" function starts us up when 'begin' is clicked
def go(Event=None):
    E1.focus_set()
    intro.set("")
    B1.destroy()
# ______________________________________________________________
# For most of the number we use this function to build our string
def resolvetest(_lefttest, _string, ):
    global AmtLeft, RN
    if AmtLeft >= _lefttest:
        RN = RN + _string
        AmtLeft = AmtLeft - _lefttest
# ______________________________________________________________
# "cbk" function is the event handler for when the return key is pressed
def cbk(event=None):
    global NumIn, RN, AmtLeft, answer
    RN = ""  # Roman Numeral - build string here
    getnum = E1.get()
    NumIn = int(getnum)
    AmtLeft = NumIn
# ______________________________________________________________
# This is the actual conversion routine.
    #Get the thousands out of the way
    while AmtLeft >= 1000:
        RN = RN + "M"
        AmtLeft = AmtLeft - 1000
    # We put the break points in a table, and break points with paired string parts in a dictionary
    rtt = (900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 5,4,3,2,1)
    rtd = {900: "CM", 800: "DCCC", 700: "DCC", 600: "DC", 500: "D", 400: "CD", 300: "CCC", 200: "CC", 100: "C",
           90: "XC", 80: "LXXX", 70: "LXX", 60: "LX", 50: "L", 40: "XL", 30: "XXX", 20: "XX", 10: "X", 9: "IX", 5:"V", 4:"IV", 3:"III", 2:"II", 1:"I"}

    for index in range(len(rtt)):  # we send each breakpoint and its concomitant string to the resolvetest function
        resolvetest(rtt[index], rtd[rtt[index]])
    answer.set(RN)
# ______________________________________________________________
ttk.Style().theme_use("alt")

# create Ap label - it what we do
L2 = ttk.Label(root, text="Arabic to Roman Numberal Converter", font=('Algerian', '24', 'bold'))
L2.pack(pady=30)

# create an entry widget, get a number to convert
E1 = ttk.Entry(root, font=('Georgia', '20', 'bold'), width=30, textvariable=intro)
E1.pack(padx=20, pady=60)
#E1.focus_set()

#lable to display the answer
L3 = ttk.Label(root, textvariable=answer, font=('Georgia', '24', 'bold'), relief="sunken", width=28, background="white", foreground="maroon")
L3.pack(pady=30)

#start button
B1 = ttk.Button(root,textvariable = begin, padding=(10,10,10,10))
B1.pack(pady=100, padx=100, ipadx=50, ipady=50)
B1.focus_set()
ttk.Style().configure('TButton',font='Arial 36', foreground='yellow', background="brown")
B1.bind('<Button-1>', go)

# bind return key to trigger event handler
root.bind('<Return>', cbk)

root.mainloop()