【问题标题】:Implementing a switch case statement that uses multiple arguments to validate register input in python tkinter在 python tkinter 中实现一个使用多个参数来验证寄存器输入的 switch case 语句
【发布时间】:2021-06-07 23:08:21
【问题描述】:

您好,我是 python 和一般编程的早期初学者。我目前正在我的 tkinter 程序中开发一个功能,该功能在注册之前验证用户输入。我已经设法使用 else if 语句编写了这样一个正在工作的函数。然而,许多条件导致了相对较长的 else if 语句块。我尝试使用字典将 else if 替换为 switch case 语句。但我不知道如何在这种情况下实现一个,因为我必须使用多个参数。下面是我的代码的工作部分和我尝试的 switch case 语句。我希望有人可以帮助我。

这显示了注册小部件

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)

检查所有条件的函数,然后显示错误输入或调用寄存器执行函数,调用它的按钮位于底部

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 screenshot of the gui for easier visualization

我尝试解决问题,如何将所有四个必需参数放入 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)

【问题讨论】:

    标签: python if-statement tkinter switch-statement


    【解决方案1】:

    我会这样做:

    def checkpassword(p: str, p2 : str):
        length=len(p)
        conditions=[p != p2 ,' ' in p , p == '' , length<=8,length>=20]
        errormsg=["Passwords do not match.",
         "Password cannot contain blank.",
         "Password cannot be blank",
         "Password is less than 8 characters",
        "Password is more than 20 characters"]
        return '\n'.join([dtext for func,dtext in zip(conditions,errormsg) if func is True])
    
    
    def checkuser(u : str):
        length = len(u)
        conditions=[checkinfile(u),' ' in u, u == '', length<=8,length>=20]
        errormsg=["Username already exists.",
         "Username cannot contain blank.",
         "Username cannot be blank.",
         "Username is less than 8 characters",
        "Username is more than 20 characters"]
        return '\n'.join([dtext for func,dtext in zip(conditions,errormsg) if func is True])
    
    def checkinfile(u : str):
        with open("accounts_list") as f:
            if u in f.read():
                return True
        return False
    
    def validate_and_submit():
        u = register_entry_username.get()
        p,p2 = register_entry_password.get(), confirm_entry_password.get()
        erroruser, errorpassword = checkuser(u),checkpassword(p,p2)
        if any([erroruser,errorpassword]):
            show_bad_input(erroruser + errorpassword)
        else:
            perform_registration(u,p)
    
    
    def perform_registration(u, p):
        ''' do wheatever is needed when all contitions are met'''
    

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 2021-02-19
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多