from Tkinter import * # use Tkinter GUI toolkit, import it entirely into this namespace import math import tkMessageBox import myhypotenuse class hypotenuseGUI: # here's the GUI code, separate from the machinery of the calculation def __init__(self, master): self.mainframe = Frame(master) # make a frame to be our main window # Because we imported the Tkinter module entirely, # we don't have to use Tkinter.Frame self.mainframe.pack() frame1 = Frame(self.mainframe) # make a subframe to group elements for layout frame1.pack(side=TOP) Label(frame1, text="Side 1 ", width=50).pack(side=LEFT) # just a visual label side1 = StringVar(master) # use a string variable, convert later into numerical data Entry(frame1, variable=side1).pack(side=LEFT) # data entry field Label(frame1, text="Side 2 ", width=50).pack(side=LEFT) side2 = StringVar(master) Entry(frame1, variable=side2).pack(side=LEFT) frame2 = Frame(self.mainframe) frame2.pack(side=TOP) Button(frame2, text="Calculate Hypotenuse", command=self.getHypotenuse).pack(side=LEFT) self.answer = Label(frame2, text="answer goes here", width=50) self.answer.pack(side=LEFT) Button(frame2, text=quit, command=onQuit).pack(side=LEFT) def getHypotenuse(self): try: value1 = float(side1.get()) value2 = float(side2.get()) except ValueError: # something couldn't be interpreted as a floating point value tkMessageBox.showinfo("Error", "Unacceptable value for side of triangle") else: hyp1 = myhypotenuse.hypotenuse(value1, value2) hyp_answer = hyp1.calculate() self.answer.text = hyp_answer def onQuit(self): self.mainframe.quit() root = Tk() # define a root GUI object/window app = hypotenuseGUI(root) # create an instance of the GUI class based on the root root.mainloop() # start the GUI event loop