简体   繁体   English

如何在 Python 中调试“TypeError:'StringVar' 类型的对象没有 len()”?

[英]How to debug “TypeError: object of type 'StringVar' has no len()” in Python?

Here is the error from the Python 3 shell:这是来自 Python 3 shell 的错误:

##

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\DMECHCH\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/DMECHCH/OneDrive/Courses ARA/Diploma IT Tech Support Courses/DTEC501 - Programming fundementals/Week 14 Session 1/14-3 Q1.py", line 119, in <lambda>
    check_btn = Button(root_window,text="Change Password",command=lambda: validate_password(pwd1, pwd2))
  File "C:/Users/DMECHCH/OneDrive/Courses ARA/Diploma IT Tech Support Courses/DTEC501 - Programming fundementals/Week 14 Session 1/14-3 Q1.py", line 27, in validate_password
    pwd_length = len(password)
TypeError: object of type 'StringVar' has no len()
##

The error occurs when I click the button单击按钮时发生错误

Here is the full code这是完整的代码

##


# Functions

def validate_password(first_pwd, second_pwd):
    """
    validates if password is acceptable
    """

    min_length = 8

    number_of_tests = 6
    test1 = 0 #Both entered passwords are identical.
    test2 = 0 #The password is >= 8 characters in length.
    test3 = 0 #if first and last chars are alpha then both must be different
    test4 = 0 #There are no more than 2 vowels in the password.
    test5 = 0 #The password has at least 1 alphabetic character
              #in either upper or lower case.
    test6 = 0 #Not all alphabetic characters are in the same
              #case (either all upper or all lower).

    # test1
    if first_pwd == second_pwd:
        test1 = 1

    password = first_pwd
    pwd_length = len(password)

    # test2
    if pwd_length >= 8:
        test2 = 1

    # test3

    same = 0

    if password[0].isalpha() and password[pwd_length-1].isalpha():
        if password[0] == password[pwd_length-1].upper():
            same = 1
        if password[0] == password[pwd_length-1].lower():
            same = 1
        if same == 0:
            test3 = 1

    else:
        test3 = 1


    # test4
    vowels = ["a","e","i","o","u","A","E","I","O","U"]
    count = 0
    max_vowels = 2
    for vowel in vowels:
        count = count + password.count(vowel)

    if count <= max_vowels:
        test4 = 1

    alpha_chars = []

    # test5
    for char in password:
        if char.isalpha():
            alpha_chars.append(char)
            test5 = 1


    # test6
    alpha_count = len(alpha_chars)
    upper_count = 0
    lower_count = 0
    for char in alpha_chars:
        if char == char.upper():
            upper_count = upper_count + 1
        if char == char.lower():
            lower_count = lower_count + 1

    if alpha_count != upper_count and \
       alpha_count != lower_count:
        test6 = 1

    test_count = test1 + test2 + test3 + test4 \
                 + test5 + test6

    if test_count == number_of_tests:
           return True



    return False

# Program Code

from tkinter import *

root_window = Tk()

# window title
root_window.title("Title")

# window size
root_window.geometry("400x200")

# label asking for password
pwd_label = Label(root_window, text="Please enter your password")
pwd_label.pack()

# pwd entry boxes

pwd1 = StringVar()
pwd1_text = Entry(root_window,textvariable=pwd1) 
pwd1_text.pack()

pwd2 = StringVar()
pwd2_text = Entry(root_window,textvariable=pwd2) 
pwd2_text.pack()

# pwd check button
check_btn = Button(root_window,text="Change Password",command=lambda: validate_password(pwd1, pwd2)) 
check_btn.pack() 




root_window.mainloop()

##

After the function is called I want to print out the return from that function to the shell either True or False but need to get past the error first调用函数后,我想打印出从该函数返回到外壳的 True 或 False 但需要先解决错误

I am still new to Python so my code likely needs some improvement.我还是 Python 新手,所以我的代码可能需要一些改进。

Your code is passing Tkinter StringVars to the validate_password() function.您的代码将 Tkinter StringVars 传递给 validate_password() 函数。 You need to call the get() method of the StringVar to get a Python string.您需要调用 StringVar 的get()方法来获取 Python 字符串。 Like p1 = first_pwd.get() .p1 = first_pwd.get()

In your validate_password function, you declare在您的validate_password函数中,您声明

password = first_pwd
pwd_length = len(password)

But you are passing in a StringVar as first_pwd .但是您将StringVar作为first_pwd The StringVar object has not length. StringVar对象没有长度。 You need to .get() the text before passing to the function.在传递给函数之前,您需要.get()文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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