登录后切换框架

Switch frames after login

输入正确的登录名和密码后,我正在尝试切换到新的框架。 不幸的是,一旦我输入所有详细信息,什么也没有发生。 不确定如何强制它打开那个新框架。 我可以在没有登录位的情况下在帧之间切换,但不知道将登录功能附加到它

 from tkinter import *
import tkinter.messagebox as tm
import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(LoginFrame)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class LoginFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)


        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)



        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2)



    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)

        if username == "abc" and password == "123":
            lambda: parent.switch_frame(PageOne)
        else:
            tm.showerror("Login error", "Incorrect username or password")

class PageOne(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to login page",
                  command=lambda: parent.switch_frame(LoginFrame)).pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

有几个问题。根本问题是这两行代码:

if username == "abc" and password == "123":
    lambda: parent.switch_frame(PageOne)

这不会导致页面切换。相反,它只是创建一个匿名函数,这将导致页面切换。你不想创建一个函数,你想要实际切换框架。在这种情况下,它应该如下所示:

if username == "abc" and password == "123":
    parent.switch_frame(PageOne)

但是,parent 未定义。您需要在__init__方法中保存parent,以便您可以在此处访问它。

class LoginFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        ...

    def _login_btn_clicked(self):
        ...
        if username == "abc" and password == "123":
            self.parent.switch_frame(PageOne)

如梦如幻感谢大家的帮助,下面粘贴固定代码

from tkinter import *
import tkinter.messagebox as tm
import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(LoginFrame)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class LoginFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent


        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)



        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2)



    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)

        if username == "abc" and password == "123":
            self.parent.switch_frame(PageOne)
        else:
            tm.showerror("Login error", "Incorrect username or password")

class PageOne(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to login page",
                  command=lambda: parent.switch_frame(LoginFrame)).pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()