validation in feet to meters

This commit is contained in:
Martin Putzlocher 2021-11-28 17:42:09 +01:00
parent 7a7f8441eb
commit 485f8a850f

View File

@ -1,6 +1,28 @@
from tkinter import *
from tkinter import ttk
status = 1
def switch_status(newS: int = None):
global status
if not newS:
if status == 1:
status = 2
else:
status = 1
else:
status = newS
update_statusbar()
def update_statusbar():
global status
if status == 1:
frame_statusbar['style'] = 'aFrame.TFrame'
elif status == 2:
frame_statusbar['style'] = 'bFrame.TFrame'
else:
frame_statusbar['style'] = 'bFrame.TFrame'
def calculate(*args):
try:
stringvalue = feet.get()
@ -9,24 +31,42 @@ def calculate(*args):
m_value = 0.3048 * value
m_value = round(m_value, 2)
meters.set(m_value)
switch_status(2)
except ValueError:
pass
return True
def check_entry(what):
switch_status(1)
if what == "":
# Nichts eingegeben
return True
else:
try:
# DEBUG
print(what)
float(what)
return True
except ValueError:
return False
# Hauptfenster
root = Tk()
root.title("Feet to Meters")
check_entry_wrapper = root.register(check_entry)
# 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))
mainframe.grid(column = 0, row = 0, sticky = NW)
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 = ttk.Entry(mainframe, width = 7, textvariable = feet,
validatecommand = (check_entry_wrapper, '%P'),
validate = 'key')
feet_entry.grid(column = 2, row = 1, sticky = (W, E))
# Einheit-Label für Eingabefeld der Länge in feet
@ -42,7 +82,7 @@ ttk.Label(mainframe, text = "Meter").grid(column = 3, row = 2, sticky = W)
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
meters_entry.configure(state = 'readonly') # keine Eingabe, aber selektierbar
# Button für Berechnung
@ -53,6 +93,18 @@ button_calc.grid(column = 3, row = 3, sticky = W)
for child in mainframe.winfo_children():
child.grid_configure(padx = 5, pady = 5)
# Statusbar
s = ttk.Style()
s.configure('aFrame.TFrame', background = "yellow")
s.configure('bFrame.TFrame', background = "green")
s.configure('cFrame.TFrame', background = "red")
frame_statusbar = ttk.Frame(root)
frame_statusbar['relief'] = 'sunken'
frame_statusbar['height'] = 12
frame_statusbar['style'] = 'aFrame.TFrame'
frame_statusbar.grid_propagate(0) # Feste Größe an Grid-Packer weitergeben
frame_statusbar.grid(column = 0, row = 1, sticky = W+S+E)
# Setze Fokus in Eingabefeld
feet_entry.focus()