我想知道如何让Python检查事件以查看username
和password
是否匹配,然后打印correct
(如果是)或false
if它不是。当我尝试时,结果总是false
。我需要一种方法来检查它们是否匹配。
这是我的代码:
from tkinter import *
root = Tk()
def check(event):
username = "Amras"
password = "pass"
if entry1 == username:
if entry2 == password:
print("true")
else:
print("false")
else:
print("false")
name = Label(root, text="Name: ")
password = Label(root, text="Password: ")
entry1 = Entry(root)
entry2 = Entry(root)
c = Checkbutton(root, text="Keep me logged in")
button1 = Button(root, text="Login")
button1.bind("<Button-1>", check)
name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c.grid(columnspan=2)
button1.grid(row=1, column=2)
root.mainloop()
答案 0 :(得分:1)
您不会评估放在entry
框内的文字。使用entry1.get()
等...
from Tkinter import *
root = Tk()
def check(event):
username = "Amras"
password = "pass"
# Also these if statements should be combined
if entry1.get() == username: # Use the .get() method
if entry2.get() == password: # Use the .get() method
print("true")
else:
print("false")
name = Label(root, text="Name: ")
password = Label(root, text="Password: ")
entry1 = Entry(root)
entry2 = Entry(root)
c = Checkbutton(root, text="Keep me logged in")
button1 = Button(root, text="Login")
button1.bind("<Button-1>", check)
name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c.grid(columnspan=2)
button1.grid(row=1, column=2)
root.mainloop()
在您输入Amras
和pass
后,您应该true
同样在你的代码中,import语句似乎很糟糕(Tkinter
默认带有大写),定义中有两个else
语句,缩进也是错误的。