下面的代码是我正在处理的,我正在尝试创建一个登录页面并验证用户名和密码。理想情况下,loginValidate函数应该在我单击提交按钮时调用,但它甚至在我这样做之前就被调用了。此外,user_login和pwd_login值似乎不会传递给函数loginValidate。请帮我解决这个问题。
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()
看看您为命令传递了什么:
loginValidate(usr_login.get(),pwd_login.get())
您正在调用函数,并将此函数返回的作为命令传递。
简单修复=使用lambda
lambda: loginValidate(usr_login.get(), pwd_login.get())