问题描述
我正在处理一个涉及用户名和密码输入框的项目,这两个输入框都必须是某个短语,以便用户进入程序的下一阶段。目前,当按下 PushButton 时,无论输入的用户名和密码是否正确,用户总是会收到“抱歉,您的用户名或密码不正确”信息框,而不是成功的信息框。
到目前为止我的程序如下。请忽略传递的变量,后面的东西很重要,所有的空白文本都是为了强制其他小部件到正确的位置。
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])
text = Text(app,text=" ",2])
input_Box1 = TextBox(app,3],height=1,width=25)
text = Text(app,text="Username",4])
input_Box2 = TextBox(app,5],hide_text=True,text="Password",text=" ",6])
submit = PushButton(app,enabled=True,command=authenticate,width=6,7])
text = Text(app,text="Submit",7],text=" ",grid = [3,11])
picture = Picture(app,image="D:\python files\logtext.gif",12])
app.display()
我知道这有点乱,但非常感谢所有帮助。
解决方法
input_box1
和 input_box2
是两个不同的 TextBox
,因此它们不会直接等于文本字符串。你想要比较它们的值:
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.")