The below code is what i am working on, I am trying to create a login page and validate username and password. Ideally loginValidate function should be invoked when i click on the submit button, but it is invoked even before i do so. Also the user_login and pwd_login values doesn't seems to be passed to the function loginvalidate. Kindly help me in resolving the issue.
import tkinter as tk
import tkinter.messagebox as tm
from tkinter import *
import sys
FONTT = ("Times", "12", "bold italic")
class myApp(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0,weight = 1)
container.grid_columnconfigure(0,weight = 1)
self.frames = {}
frame = LoginPage(container,self)
self.frames[LoginPage] = frame
frame.grid(row = 0,column = 0,sticky = "nsew")
self.show_frame(LoginPage)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
def loginValidate(user,pwd):
if(user == "yogesh" and pwd == "123456"):
print("1")
else:
print("2")
tm.showerror("Login error", "Incorrect username or password")
class LoginPage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
usr_login = StringVar()
pwd_login = StringVar()
userLabel = tk.Label(self,text = "Name",font = FONTT )
passwordLabel = tk.Label(self,text = "Password", font = FONTT)
userEntry = tk.Entry(self, textvariable = usr_login, bd=5)
passwordEntry = tk.Entry(self, textvariable=pwd_login,bd=5,show = "*")
submitButton = tk.Button(self,text = "Login",command = lambda: loginValidate(usr_login.get(),pwd_login.get()))
quitButton = tk.Button(self,text = "Quit",command =lambda: app.destroy)
userLabel.grid(row = 0,sticky = "E",padx =10,pady =10)
passwordLabel.grid(row =1,sticky = "E",padx =10,pady =10)
userEntry.grid(row=0,column=1,padx =10,pady =10)
passwordEntry.grid(row=1,column=1,padx =10,pady =10)
submitButton.grid(row =2,column =1,padx =10,pady =10)
quitButton.grid(row=2,column=2,padx =10,pady =10)
app = myApp()
app.mainloop()
Look at what you've passed for the command:
loginValidate(usr_login.get(),pwd_login.get())
You're calling the function and passing what this function returns as the command.
Simple fix = use lambda
lambda: loginValidate(usr_login.get(), pwd_login.get())