followed brief tutorial
This commit is contained in:
parent
d3b1d30f8b
commit
430e7e37be
67
tkinter tutorial/tkinter unit converter.py
Normal file
67
tkinter tutorial/tkinter unit converter.py
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
from tkinter import *
|
||||||
|
from tkinter import ttk
|
||||||
|
|
||||||
|
root = Tk()
|
||||||
|
root.title("Distance conversion")
|
||||||
|
|
||||||
|
factors = { #all values are m / x
|
||||||
|
"m" : 1,
|
||||||
|
"cm" : 0.01,
|
||||||
|
"mm" : 0.001,
|
||||||
|
"km" : 1000,
|
||||||
|
"au" : 1.496e11,
|
||||||
|
"ly" : 9.461e15,
|
||||||
|
"pc" : 3.086e16,
|
||||||
|
"ft" : 0.3048,
|
||||||
|
"yd" : 0.9144,
|
||||||
|
"in" : 0.0254
|
||||||
|
}
|
||||||
|
|
||||||
|
def calculate(*args):
|
||||||
|
try:
|
||||||
|
value = float(entry.get())
|
||||||
|
destUnit = 1/factors[destUnitString.get()]
|
||||||
|
srcUnit = factors[srcUnitString.get()]
|
||||||
|
factor = srcUnit*destUnit
|
||||||
|
out.set(round(value*factor, 4))
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
bg = PhotoImage(file="M:\Pictures\hk stream\hornet siitt.png")
|
||||||
|
im = ttk.Label(root, image=bg)
|
||||||
|
im.grid(column=0,row=1)
|
||||||
|
|
||||||
|
mainframe = ttk.Frame(root, padding=(5,15,5,15))
|
||||||
|
mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
|
||||||
|
|
||||||
|
entry = StringVar()
|
||||||
|
entryBox = ttk.Entry(mainframe, width=8, textvariable=entry)
|
||||||
|
entryBox.grid(column=2,row=1, sticky=(W,E))
|
||||||
|
|
||||||
|
out = StringVar()
|
||||||
|
ttk.Label(mainframe, textvariable=out).grid(column=2, row=2, sticky=(W,E))
|
||||||
|
|
||||||
|
calcButton = ttk.Button(mainframe, text="Convert", command=calculate)
|
||||||
|
calcButton.grid(column=3, row=3, sticky=W)
|
||||||
|
|
||||||
|
srcUnitString = StringVar()
|
||||||
|
srcUnitString.set("ft")
|
||||||
|
srcUnitEntry = ttk.Combobox(mainframe, textvariable=(srcUnitString), values=list(factors.keys()), state="readonly")
|
||||||
|
srcUnitEntry.grid(column=3, row=1, sticky=W)
|
||||||
|
|
||||||
|
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
|
||||||
|
|
||||||
|
destUnitString = StringVar()
|
||||||
|
destUnitString.set("m")
|
||||||
|
destUnitEntry = ttk.Combobox(mainframe, textvariable=destUnitString, values=list(factors.keys()), state="readonly")
|
||||||
|
destUnitEntry.grid(column=3, row=2, sticky=W)
|
||||||
|
|
||||||
|
root.columnconfigure(0, weight=1)
|
||||||
|
root.rowconfigure(0, weight=1)
|
||||||
|
mainframe.columnconfigure(2, weight=1)
|
||||||
|
for child in mainframe.winfo_children():
|
||||||
|
child.grid_configure(padx=5, pady=5)
|
||||||
|
entryBox.focus()
|
||||||
|
root.bind("<Return>", lambda e: calcButton.invoke())
|
||||||
|
|
||||||
|
root.mainloop()
|
||||||
Loading…
Reference in a new issue