Hello Iam an early beginner in python and programing in general. Iam currently working on a function in my tkinter programm that validates user input before the registration. I have already managed to successfully write such a function using else if statements, however the many conditions have caused a relatively long block of else if statements. I tried to replace the else if's with switch case statements using a dictionary. But I can't figure out how to implement one in this situation because I have to use multiple arguments. Below is the working section of my code and my attemtpted switch case statement. I hope someone can help me out.
this shows the register widgets
def register_widgets_show():
register_text1 = Label(root, text="Enter your username", font="Arial 14 bold")
register_text1.grid(row=8, column=0)
register_text2 = Label(root, text="\nEnter your password", font="Arial 14 bold")
register_text2.grid(row=10, column=0)
register_text3 = Label(root, text="\nConfirm your password", font="Arial 14 bold")
register_text3.grid(row=12, column=0)
register_entry_username = Entry(root, width=30, relief="sunken", bg="light grey", font="Arial 12")
register_entry_username.grid(row=9, column=0)
register_entry_password = Entry(root, width=30, relief="sunken", bg="light grey", font="Arial 12", show="*")
register_entry_password.grid(row=11, column=0)
confirm_entry_password = Entry(root, width=30, relief="sunken", bg="light grey", font="Arial 12", show="*")
confirm_entry_password.grid(row=13, column=0)
the function that checks all conditions, it then either shows bad input or calls the register execution function the button calling it is at the bottom
def register_validation():
username_not_valid = register_entry_username.get()
if register_entry_password.get() != confirm_entry_password.get():
show_bad_input("Passwords do not match.")
elif register_entry_username.get() == "":
show_bad_input("Name cannot be blank.")
elif register_entry_password.get() == "":
show_bad_input("You must set a password.")
elif len(register_entry_password.get()) < 6:
show_bad_input("Password must be at least 6 characters long.")
elif username_not_valid in open("accounts_list").read():
show_bad_input("Username already exists.")
elif " " in register_entry_username.get():
show_bad_input("Name cannot contain blank space.")
elif " " in register_entry_password.get():
show_bad_input("Password cannot contain blank space.")
elif len(register_entry_username.get()) > 20:
show_bad_input("Name cannot be longer than 20 characters.")
elif register_entry_password.get() == confirm_entry_password.get(): register_execute()
def show_bad_input(error):
bad_input = Label(root, text=error, fg="red", font="Arial 12 bold")
bad_input.grid(row=16, column=0)
bad_input.after(3000, lambda: bad_input.destroy())
def register_execute():
create_account_button = Button(root, text="Create account", bg="light grey",
font="Arial 12 bold",command=register_validation)
create_account_button.grid(row=15, column=0)
a picture of the gui for easier visualization
my attempt to solve the problem, how do I put all three required arguments into show_bad_input_or_register_execute ?
def register_validation(error):
username_not_valid = register_entry_username.get()
return {
register_entry_password.get() != confirm_entry_password.get(): "Passwords do not match.",
register_entry_username.get() == "": "Name cannot be blank.",
register_entry_password.get() == "": "You must set a password.",
len(register_entry_password.get()) < 6: "Password must be at least 6 characters long.",
username_not_valid in open("accounts_list").read(): "Username already exists.",
" " in register_entry_username.get(): "Name cannot contain blank space.",
" " in register_entry_password.get(): "Password cannot contain blank space.",
len(register_entry_username.get()) > 20: "Name cannot be longer than 20 characters.",
register_entry_password.get() == confirm_entry_password.get(): register_execute()
}.get(error)
def show_bad_input_or_register_execute():
bad_input = Label(root, text=register_validation(register_entry_username.get()), fg="red", font="Arial 12 bold")
bad_input.grid(row=16, column=0)
bad_input.after(3000, lambda: bad_input.destroy())
def register_execute():
create_account_button = Button(root, text="Create account", bg="light grey",
font="Arial 12 bold",command=show_bad_input_or_register_execute)
create_account_button.grid(row=15, column=0)
Aucun commentaire:
Enregistrer un commentaire