函数检查“ if”语句的问题

我正在尝试设计一个简单的密码系统。它与检查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,"/5 attempts")
        text.insert(INSERT,"\naccess denied\n")
        text.pack()
qwer879 回答:函数检查“ if”语句的问题

您可能希望从条目中获取文本,如下所示:

if entry.get()==password and number_of_guesses<5:

为说明起见,您正在将Entry对象(一组数据和函数)与文本进行比较。您必须通过.get()指定要使用文本值。有关Entry小部件的更多信息,请参阅Documentation of Entry Widget

本文链接:https://www.f2er.com/3153534.html

大家都在问