Feet to Meters example

This commit is contained in:
Martin Putzlocher 2021-11-02 12:35:51 +01:00
parent 883053c716
commit 7a7f8441eb

62
tk_first_steps/feet.py Normal file
View File

@ -0,0 +1,62 @@
from tkinter import *
from tkinter import ttk
def calculate(*args):
try:
stringvalue = feet.get()
stringvalue = stringvalue.replace(",",".")
value = float(stringvalue)
m_value = 0.3048 * value
m_value = round(m_value, 2)
meters.set(m_value)
except ValueError:
pass
return True
# Hauptfenster
root = Tk()
root.title("Feet to Meters")
# Rahmen im Hauptfenster (aus ttk für Farbanpassung)
mainframe = ttk.Frame(root, padding = "3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))
root.columnconfigure(0, weight = 1)
root.rowconfigure(0, weight = 1)
# Eingabefeld für Länge in feet
feet = StringVar()
feet_entry = ttk.Entry(mainframe, width = 7, textvariable = feet)
feet_entry.grid(column = 2, row = 1, sticky = (W, E))
# Einheit-Label für Eingabefeld der Länge in feet
ttk.Label(mainframe, text = "feet").grid(column = 3, row = 1, sticky = W)
# Label für Ausgabefeld der Länge in Metern
ttk.Label(mainframe, text = "ist äquivalent zu").grid(column = 1, row = 2, sticky = E)
# Einheit-Label für Ausgabefeld der Länge in Metern
ttk.Label(mainframe, text = "Meter").grid(column = 3, row = 2, sticky = W)
# Ausgabefeld für Länge in Metern
meters = StringVar()
meters_entry = ttk.Entry(mainframe, width = 7, textvariable = meters)
meters_entry.grid(column = 2, row = 2, sticky = (W,E))
meters_entry.configure(state = 'disabled') # keine Eingabe, aber selektierbar
# Button für Berechnung
button_calc = ttk.Button(mainframe, text = "Berechne", command = calculate)
button_calc.grid(column = 3, row = 3, sticky = W)
# schönere Abstände
for child in mainframe.winfo_children():
child.grid_configure(padx = 5, pady = 5)
# Setze Fokus in Eingabefeld
feet_entry.focus()
root.bind("<Return>", calculate)
root.bind("<KP_Enter>", calculate)
root.mainloop()