2021-11-02 11:35:51 +00:00
|
|
|
from tkinter import *
|
|
|
|
from tkinter import ttk
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
status = 1 # 1 - neue Eingabe, 2 - Eingabe verarbeitet
|
2021-11-28 16:42:09 +00:00
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
|
|
|
|
def switch_status(new_status: int = None):
|
2021-11-28 17:02:11 +00:00
|
|
|
""" Verändert den globalen Status
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
:param new_status: neuer Statuswert
|
2021-11-28 17:02:11 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
2021-11-28 16:42:09 +00:00
|
|
|
global status
|
2021-11-28 17:22:11 +00:00
|
|
|
if not new_status:
|
2021-11-28 16:42:09 +00:00
|
|
|
if status == 1:
|
|
|
|
status = 2
|
|
|
|
else:
|
|
|
|
status = 1
|
|
|
|
else:
|
2021-11-28 17:22:11 +00:00
|
|
|
status = new_status
|
2021-11-28 16:42:09 +00:00
|
|
|
update_statusbar()
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
|
2021-11-28 16:42:09 +00:00
|
|
|
def update_statusbar():
|
2021-11-28 17:02:11 +00:00
|
|
|
""" Passt die Farbe der Statuszeile an den Status an.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
2021-11-28 16:42:09 +00:00
|
|
|
global status
|
|
|
|
if status == 1:
|
|
|
|
frame_statusbar['style'] = 'aFrame.TFrame'
|
|
|
|
elif status == 2:
|
|
|
|
frame_statusbar['style'] = 'bFrame.TFrame'
|
|
|
|
else:
|
|
|
|
frame_statusbar['style'] = 'bFrame.TFrame'
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
|
2021-11-02 11:35:51 +00:00
|
|
|
def calculate(*args):
|
2021-11-28 17:02:11 +00:00
|
|
|
""" Berechnet aus der eingegebenen Länge in feet die Länge in Metern.
|
|
|
|
|
|
|
|
:param args:
|
|
|
|
:return: None
|
|
|
|
"""
|
2021-11-02 11:35:51 +00:00
|
|
|
try:
|
|
|
|
stringvalue = feet.get()
|
2021-11-28 17:22:11 +00:00
|
|
|
stringvalue = stringvalue.replace(",", ".")
|
2021-11-02 11:35:51 +00:00
|
|
|
value = float(stringvalue)
|
|
|
|
m_value = 0.3048 * value
|
|
|
|
m_value = round(m_value, 2)
|
|
|
|
meters.set(m_value)
|
2021-11-28 16:42:09 +00:00
|
|
|
switch_status(2)
|
2021-11-02 11:35:51 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return True
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
|
|
|
|
def check_entry(what: str = ""):
|
2021-11-28 17:02:11 +00:00
|
|
|
""" Validiert, ob Eingabe leer oder eine Fließkommazahl ist.
|
|
|
|
|
|
|
|
:param what: str eingegebene Zeichenkette
|
|
|
|
:return: boolean
|
|
|
|
"""
|
2021-11-28 16:42:09 +00:00
|
|
|
switch_status(1)
|
|
|
|
if what == "":
|
|
|
|
# Nichts eingegeben
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
# DEBUG
|
|
|
|
print(what)
|
2021-11-28 17:02:11 +00:00
|
|
|
what = what.replace(',', '.')
|
2021-11-28 16:42:09 +00:00
|
|
|
float(what)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
|
2021-11-02 11:35:51 +00:00
|
|
|
# Hauptfenster
|
|
|
|
root = Tk()
|
|
|
|
root.title("Feet to Meters")
|
2021-11-28 16:42:09 +00:00
|
|
|
check_entry_wrapper = root.register(check_entry)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Rahmen im Hauptfenster (aus ttk für Farbanpassung)
|
2021-11-28 17:22:11 +00:00
|
|
|
mainframe = ttk.Frame(root, padding="3 3 12 12")
|
2021-11-02 11:35:51 +00:00
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
mainframe.grid(column=0, row=0, sticky=NW)
|
|
|
|
root.columnconfigure(0, weight=1)
|
|
|
|
root.rowconfigure(0, weight=1)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Eingabefeld für Länge in feet
|
|
|
|
feet = StringVar()
|
2021-11-28 17:22:11 +00:00
|
|
|
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet,
|
|
|
|
validatecommand=(check_entry_wrapper, '%P'),
|
|
|
|
validate='key')
|
|
|
|
feet_entry.grid(column=2, row=1, sticky=W + E)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Einheit-Label für Eingabefeld der Länge in feet
|
2021-11-28 17:22:11 +00:00
|
|
|
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Label für Ausgabefeld der Länge in Metern
|
2021-11-28 17:22:11 +00:00
|
|
|
ttk.Label(mainframe, text="ist äquivalent zu").grid(column=1, row=2, sticky=E)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Einheit-Label für Ausgabefeld der Länge in Metern
|
2021-11-28 17:22:11 +00:00
|
|
|
ttk.Label(mainframe, text="Meter").grid(column=3, row=2, sticky=W)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Ausgabefeld für Länge in Metern
|
|
|
|
meters = StringVar()
|
2021-11-28 17:22:11 +00:00
|
|
|
meters_entry = ttk.Entry(mainframe, width=7, textvariable=meters)
|
|
|
|
meters_entry.grid(column=2, row=2, sticky=(W, E))
|
|
|
|
meters_entry.configure(state='readonly') # keine Eingabe, aber selektierbar
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# Button für Berechnung
|
|
|
|
|
2021-11-28 17:22:11 +00:00
|
|
|
button_calc = ttk.Button(mainframe, text="Berechne", command=calculate)
|
|
|
|
button_calc.grid(column=3, row=3, sticky=W)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
|
|
|
# schönere Abstände
|
|
|
|
for child in mainframe.winfo_children():
|
2021-11-28 17:22:11 +00:00
|
|
|
child.grid_configure(padx=5, pady=5)
|
2021-11-02 11:35:51 +00:00
|
|
|
|
2021-11-28 16:42:09 +00:00
|
|
|
# Statusbar
|
|
|
|
s = ttk.Style()
|
2021-11-28 17:22:11 +00:00
|
|
|
s.configure('aFrame.TFrame', background="yellow")
|
|
|
|
s.configure('bFrame.TFrame', background="green")
|
|
|
|
s.configure('cFrame.TFrame', background="red")
|
2021-11-28 16:42:09 +00:00
|
|
|
frame_statusbar = ttk.Frame(root)
|
|
|
|
frame_statusbar['relief'] = 'sunken'
|
|
|
|
frame_statusbar['height'] = 12
|
|
|
|
frame_statusbar['style'] = 'aFrame.TFrame'
|
2021-11-28 17:22:11 +00:00
|
|
|
frame_statusbar.grid_propagate(0) # Feste Größe an Grid-Packer weitergeben
|
|
|
|
frame_statusbar.grid(column=0, row=1, sticky=W + S + E)
|
2021-11-28 16:42:09 +00:00
|
|
|
|
2021-11-02 11:35:51 +00:00
|
|
|
# Setze Fokus in Eingabefeld
|
|
|
|
feet_entry.focus()
|
|
|
|
|
|
|
|
root.bind("<Return>", calculate)
|
|
|
|
root.bind("<KP_Enter>", calculate)
|
|
|
|
|
|
|
|
root.mainloop()
|