简体   繁体   中英

tkinter check empty entry box

I am trying to build a GUI for my password generator . Here I have an entry box for the length of password ( inputPasswordLength which is int ), which is mandatory. The maximal length is set to 20. Now, if the entry box for password length is empty, a message should be displayed on screen(ResultDisplay). In all posts it is mentioned to compare the length. So I did, and the following error is thrown:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/Volumes/HDD/Users/Stephan/PycharmProjects/passwordGeneratorGUIbeta/main.py", line 28, in PasswordGenerationFunc
    if len(passwordLength) == 0:
TypeError: object of type 'int' has no len()

Expected: If the entry for password length is empty, then show message to input a length.

My code:

# GENERATE PASSWORD FUNCTION
def PasswordGenerationFunc():
    password = None
    passwordLength = inputPasswordLength.get()
    passwordLength = int(passwordLength)
    userName = inputUsername.get()

    if len(passwordLength) == 0:
        ResultDisplay.configure(text="Length of password is mandatory.", fg="red")
    else:
        if passwordLength > maxPasswordLength:
            ResultDisplay.configure(text="The limit of password length are 20 characters.", fg="red")
        else:
            if userName != "":
                password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
                ResultDisplay.configure(text="Generated password for " + userName + " is:\n" + password, fg="white")
            else:
                password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
                ResultDisplay.configure(text="Generated password is: \n" + password, fg="white")

I appreciate your help. Thanks in advance.

thanks for your help. Now it works without any problems. I don't know if this is dirrty style, but this is how I fixed it:

def PasswordGenerationFunc():
    password = None
    passwordLength = inputPasswordLength.get()
    userName = inputUsername.get()

    if len(passwordLength) == 0:
        ResultDisplay.configure(text="Length of password is mandatory.", fg="red")
    else:
        passwordLength = int(passwordLength)
        if passwordLength > maxPasswordLength:
            ResultDisplay.configure(text="The limit of password length are 20 characters.", fg="red")
        else:
            if userName != "":
                password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
                ResultDisplay.configure(text="Generated password for " + userName + " is:\n" + password, fg="white")
            else:
                password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
                ResultDisplay.configure(text="Generated password is: \n" + password, fg="white")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM