So, here is the code (gist):
#! /usr/bin/env python3
# include tkinter packages
from tkinter import *
from tkinter.ttk import *
# create tk root class
root = Tk()
# create frame, add title and setup grid
frame = Frame(root, padding='3 3 12 12')
frame.master.title('Feet to meters')
frame.grid(column=0, row=0, sticky=NW)
frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1)
# text entry
frame.feet = Entry(frame, width=7)
frame.feet.grid(column=2, row=1, sticky=W)
# calculated meters label
frame.meters = Label(frame, text='')
frame.meters.grid(column=2, row=2, sticky=W)
# calculate callback
def calculate(*e):
try:
frame.meters.config(
text = str(0.3048 * int(frame.feet.get()) *
10000.0/10000.0))
except:
frame.meters.config(text="")
# calculate button
frame.calc = Button(frame, text='Calculate', command = calculate)
frame.calc.grid(column=3, row=3, sticky=W)
# feet label
frame.flbl = Label(frame, text='feet')
frame.flbl.grid(column=3, row=1, sticky=W)
# is eq label
frame.islbl = Label(frame, text='is equivalent to')
frame.islbl.grid(column=1, row=2, sticky=E)
# meters label
frame.mlbl = Label(frame, text='meters')
frame.mlbl.grid(column=3, row=2, sticky=W)
# distance the widgets a bit
for w in frame.children.values():
w.grid(padx=5, pady=5)
# set focus to feet entry
frame.feet.focus()
# bindto calculate
frame.calc.bind("<Return>", calculate)
# enter main loop
root.mainloop()
No comments:
Post a Comment