Tkinter - 如果输入正确的信息,则更改帧

时间:2015-04-03 12:41:30

标签: python oop tkinter

我对Tkinter来说相当新,所以我可能忽略了某些事情或者犯了一个简单的错误。我有两个框架:一个登录屏幕和另一个带有标签的框架。 我想在输入正确的登录详细信息时更改帧。如果他们输入了错误的登录详细信息,则输出错误消息,这样可以正常工作,但是当输入正确的详细信息时,没有任何反应,没有错误,没有任何改变:

import tkinter as tk
import tkinter.messagebox as tm

class QPCSRegistration(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 = {}
        for F in (LoginScreen,TestScreen):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            self.show_frame(LoginScreen)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class LoginScreen(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.Username_label = tk.Label(self, text="Username")
        self.Password_label = tk.Label(self, text="Password")

        self.Username_entry = tk.Entry(self)
        self.Password_entry = tk.Entry(self, show="*")

        self.Username_label.grid(row=0, sticky="e")
        self.Password_label.grid(row=1, sticky="e")
        self.Username_entry.grid(row=0, column=1)
        self.Password_entry.grid(row=1, column=1)


        self.LoginButton = tk.Button(self, text="Login", command = self.Login_Button_Clicked)
        self.LoginButton.grid(row=2, column=1)

        self.pack()

    def Login_Button_Clicked(self):

        Username = self.Username_entry.get()
        Password = self.Password_entry.get()

        if Username == "Victoria" and Password == "qpcsadmin":
            lambda:controller.show_frame(TestScreen)
        else:
            tm.showerror("Login error", "Incorrect Login details")

class TestScreen(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.Random_label = tk.Label(self, text="Random Label")

app = QPCSRegistration()
app.mainloop()

任何人都知道为什么我的代码不起作用?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

    if Username == "Victoria" and Password == "qpcsadmin":
        lambda:controller.show_frame(TestScreen)

删除那里的lambda

    if Username == "Victoria" and Password == "qpcsadmin":
        controller.show_frame(TestScreen)

您可能还必须将controller保存为self的属性,否则它将无法在__init__之外的任何位置访问。

def __init__(self, parent, controller):
    self.controller = controller
    #etc
def Login_Button_Clicked(self):
    #etc
        self.controller.show_frame(TestScreen)
    #etc