from tkinter import *
#Windows
logWin = Tk()
mainWin = Tk()
srchWin = Tk()
NTWin = Tk()
#Variables
userName="123"
password="123"
logFail = ""
userBox = Entry(logWin)
passBox = Entry(logWin)
EU = userBox.get()
EP = passBox.get()
#General Window
mainWin.withdraw()
srchWin.withdraw()
NTWin.withdraw()
#Command
def loginCmd():
if EU == userName and EP == password:
print ("hello")
else:
print("no")
#Login Window
logWin.title("Login")
logWin.geometry("200x70")
userBox.grid(row=0,column=1)
passBox.grid(row=1,column=1)
userLbl = Label(logWin,text="Username:")
userLbl.grid(row=0,column=0)
passLbl = Label(logWin,text="Password:")
passLbl.grid(row=1,column=0)
failLbl = Label(logWin,text=logFail)
failLbl.grid(row=2,column=0)
logBtn = Button(logWin,text="Login",command=loginCmd)
logBtn.grid(row=2,column=1)
mainloop()
我正在尝试创建一个程序,要求您登录,但是命令if
中的def
语句似乎无效,我也不知道为什么。
答案 0 :(得分:2)
为使其正常工作,您需要在单击按钮时获得价值,而不是在初始化文本框时获得价值。因此,如果将登录功能更改为以下内容,则程序应能按预期运行。
def loginCmd():
if userBox.get() == userName and passBox.get() == password:
print("hello")
else:
print("no")