From 7a7f8441eba396faeeef579b01f7200722188310 Mon Sep 17 00:00:00 2001 From: Martin Putzlocher Date: Tue, 2 Nov 2021 12:35:51 +0100 Subject: [PATCH] Feet to Meters example --- tk_first_steps/feet.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tk_first_steps/feet.py diff --git a/tk_first_steps/feet.py b/tk_first_steps/feet.py new file mode 100644 index 0000000..08fe1a4 --- /dev/null +++ b/tk_first_steps/feet.py @@ -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("", calculate) +root.bind("", calculate) + +root.mainloop()