我正在尝试设计一个简单的密码系统。它与检查 3 个 if 语句的函数一起使用。问题是即使密码正确,它也不会运行第一个 if 语句。 似乎变量 entry 永远不会等于变量 password,但我不知道为什么。
##user input space
entry=Entry(top,bd=5)
entry.pack(side=LEFT)
##actual password to be set
password="abc"
number_of_guesses=0
##function to check user input
def r():
global entry,password,number_of_guesses
number_of_guesses+=1
if entry==password and number_of_guesses<5:
root=Tk()
text=Text(root)
text.insert(INSERT,"You shall pass\nAttempts used:")
text.insert(INSERT,number_of_guesses)
text.pack()
elif number_of_guesses<5 and number_of_guesses>0:
root=Tk()
text=Text(root)
text.insert(INSERT,"Wrong password\nYou used ")
text.insert(INSERT,number_of_guesses)
text.insert(INSERT,"/5 attempts")
text.pack()
elif number_of_guesses>=5:
root=Tk()
text=Text(root)
text.insert(INSERT,"Wrong password\nYou used ")
text.insert(INSERT,number_of_guesses)
text.insert(INSERT,"/5 attempts")
text.insert(INSERT,"\nAccess denied\n")
text.pack()
请您参考如下方法:
您可能希望从条目中获取如下文本:
if entry.get()==password and number_of_guesses<5:
为了解释,您将 Entry 对象(它是一组数据和函数)与文本进行比较。您必须指定要通过 .get() 使用文本值。有关条目小部件的更多信息,请参阅 Documentation of Entry Widget .
