from Tkinter import * import math
root = Tk() top = Frame(root) top.pack(side='top')
font = 'times 18 bold' hwtext = Label(top, text='Hello, World!', font=font) hwtext.grid(row=0, column=0, columnspan=4, pady=20)
r_label = Label(top, text='The sine of') r_label.grid(row=1, column=0)
r = StringVar() r.set('1.2') r_entry = Entry(top, width=6, textvariable=r) r_entry.grid(row=1, column=1)
s = StringVar() def comp_s(event=None): global s s.set('%g' % math.sin(float(r.get()))) # construct string
r_entry.bind('<Return>', comp_s)
compute = Button(top, text=' equals ', command=comp_s, relief='flat') compute.grid(row=1, column=2)
s_label = Label(top, textvariable=s, width=12) s_label.grid(row=1, column=3)
def quit(event=None): root.destroy() quit_button = Button(top, text='Goodbye, GUI World!', command=quit, background='yellow', foreground='blue') quit_button.grid(row=2, column=0, columnspan=4, pady=5, sticky='ew') root.bind('<q>', quit)
root.mainloop()
|