[英]How do i solve this error TypeError: object of type 'StringVar' has no len()
[英]How to debug “TypeError: object of type 'StringVar' has no len()” in Python?
這是來自 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()
##
單擊按鈕時發生錯誤
這是完整的代碼
##
# 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()
##
調用函數后,我想打印出從該函數返回到外殼的 True 或 False 但需要先解決錯誤
我還是 Python 新手,所以我的代碼可能需要一些改進。
您的代碼將 Tkinter StringVars 傳遞給 validate_password() 函數。 您需要調用 StringVar 的get()
方法來獲取 Python 字符串。 像p1 = first_pwd.get()
。
在您的validate_password
函數中,您聲明
password = first_pwd
pwd_length = len(password)
但是您將StringVar
作為first_pwd
。 StringVar
對象沒有長度。 在傳遞給函數之前,您需要.get()
文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.