Something goes wrong in the check of the email address if empty
cat figure out whats wrong, while the age check works
Thanks for any, help, hint
Regards
Rainer
"""
test project
erst benötigte Module laden
"""
import tkinter as tk # für die GUI
from tkinter import ttk # bessere widgets für die GUI
import sqlite3 as sql # für die Datenbank
from string import digits # alle Zahlen von 0 bis 9
import re
arial_20 =("Arial",18)
MIN_AGE = 10
MAX_AGE = 99
""" check if correct email address """
def valid_email(email_input):
return bool(re.match( r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',email_input))
def is_email():
test = ent_email.get()
if not test == "":
if valid_email(test):
return True
else:
ent_email.delete(0, tk.END)
ent_email.focus_set()
lbl_email_info.configure(text= "Ungültige Email Adresse")
return False
else:
ent_email.focus_set()
# lbl_email_info.configure(text="Pflichtfeld")
return False
""" check if age in range """
def check_age():
try:
value = int(ent_age.get())
""" here if it is INT """
if MIN_AGE <= value <= MAX_AGE:
lbl_age_info.configure(text="OK")
# lbl_gender.focus_set()
return True
else:
lbl_age_info.configure(text=f"Alter {value} ungültig")
ent_age.delete(0, tk.END)
ent_age.focus_set()
#return False
except:
ValueError: "nn"
lbl_age_info.configure(text="Nur Ziffern")
ent_age.delete(0,tk.END)
ent_age.focus_set()
#return False
return False
root = tk.Tk()
root.title('User im System')
root.geometry("800x400+50+50")
root.minsize(width=900, height=400)
root.maxsize(width=1000, height=600)
""" E-MAil Address """
lbl_email = ttk.Label(root, text="E-Mail:* ", width=10, font=arial_20,padding=5)
lbl_email.grid(row=0, column=0, padx='5', pady='5')
ent_email = ttk.Entry(root,width=40,font=arial_20, justify="left", validatecommand=is_email, validate="focusout")
ent_email.grid(row=0, column=1, padx='5', pady='5',sticky="w")
lbl_email_info = ttk.Label(root, text="_", width=20, font=arial_20, padding=5)
lbl_email_info.grid(row=0, column=3, padx='5', pady='5')
""" Age """
lbl_age = ttk.Label(root, text="Alter: ", width=10, font=arial_20,padding=5)
lbl_age.grid(row=1, column=0, padx='5', pady='5')
ent_age = ttk.Entry(root,width=2,font=arial_20, validatecommand=check_age, validate="focusout")
ent_age.grid(row=1, column=1, padx='5', pady='5',sticky="w")
lbl_age_info = ttk.Label(root, text="zwischen 10 und 99", width=20, font=arial_20, padding=5)
lbl_age_info.grid(row=1, column=3, padx='5', pady='5')
root.mainloop()