paton
paton

Reputation: 13

Tkinter Button command function doesn't perform its task

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()

I am trying to create a program that requires you to login however my if statement in my def of the command doesn't seem to work and I have no idea why.

screenshot of what happens

Upvotes: 1

Views: 42

Answers (1)

The Pineapple
The Pineapple

Reputation: 567

In order for it to work, you need to get value when you click the button not when the textbox is initialized. Therefore, if you change the login function to the following, your program should function as expected.

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

Upvotes: 2

Related Questions