Tkinter Button命令功能无法执行其任务

时间:2018-11-18 04:22:06

标签: python function button tkinter

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

1 个答案:

答案 0 :(得分:2)

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

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