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语句似乎不起作用,我也不知道为什么。

screenshot of what happens

最佳答案

为了使它起作用,您需要在单击按钮时获得价值,而不是在初始化文本框时获得价值。因此,如果将登录功能更改为以下内容,则程序应能按预期运行。

def loginCmd():
    if userBox.get() == userName and passBox.get() == password:
        print("hello")
    else:
        print("no")

10-08 03:57