我在 guizero 中输入用户名和密码的脚本有什么问题?

What's wrong with my script for a username and password in guizero?

我正在做一个涉及用户名和密码输入框的项目,用户名和密码输入框都必须是特定的短语,用户才能进入程序的下一阶段。目前,当按下按钮时,无论输入的用户名和密码是否正确,用户总是会看到“抱歉,您的用户名或密码不正确”信息框,而不是成功的信息框。

我目前的程序如下。请忽略传递的变量,这对以后的事情很重要,所有空白文本都是为了强制其他小部件到正确的位置。

from guizero import App, Text, TextBox, PushButton, info, Picture

passed = False

def authenticate():
    if input_box1 == "a" and input_box2 == "b":
        passed = True
        info("Welcome", "Level 5 identification accepted. Welcome, Dr. Artyom.")
    else:
        info("Password", "Your username or password is incorrect.")


app = App(title="AppTest", width=900, height=600, layout="grid", bg=[255,255,255])

text = Text(app, text=" ", grid=[0,0])

text = Text(app, text="                                  ", grid=[1,1])

text = Text(app, text="Please enter a valid username and password.", grid=[2,1], color=[0,0,0])
    
text = Text(app, text="             ", grid=[2,2])

input_box1 = TextBox(app, grid=[2,3], height=1, width=25)

text = Text(app, text="Username", grid=[1,3], color=[0,0,0])

text = Text(app, text="             ", grid=[2,4])

input_box2 = TextBox(app, grid=[2,5], hide_text=True, height=1, width=25)

text = Text(app, text="Password", grid=[1,5], color=[0,0,0])

text = Text(app, text="        ", grid=[2,6])

submit = PushButton(app, enabled=True, command=authenticate, height=1, width=6, grid=[2,7])

text = Text(app, text="Submit", grid=[2,7], color=[0,0,0])

text = Text(app, text="          ", grid = [3,11])

picture = Picture(app, image="D:\python files\logtext.gif", grid=[2,12])


app.display()

我知道这有点乱,但非常感谢大家的帮助。

input_box1input_box2是两个不同的TextBox,所以不会直接等于文本字符串。相反,您想要比较它们的 values:

def authenticate():
    if input_box1.value == "a" and input_box2.value == "b":
        passed = True
        info("Welcome", "Level 5 identification accepted. Welcome, Dr. Artyom.")
    else:
        info("Password", "Your username or password is incorrect.")