Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 登录后切换帧_Python_Python 3.x_Authentication_Tkinter_Frames - Fatal编程技术网

Python 登录后切换帧

Python 登录后切换帧,python,python-3.x,authentication,tkinter,frames,Python,Python 3.x,Authentication,Tkinter,Frames,输入正确的登录名和密码后,我正在尝试切换到新的框架。 不幸的是,一旦我输入了所有的细节,什么都不会发生。 不知道如何强制它打开新的框架。 我同意在没有登录位的框架之间切换,但不知道如何将登录功能附加到框架上 from tkinter import * import tkinter.messagebox as tm import tkinter as tk class SampleApp(tk.Tk): def __init__(self): tk.Tk.__init_

输入正确的登录名和密码后,我正在尝试切换到新的框架。 不幸的是,一旦我输入了所有的细节,什么都不会发生。 不知道如何强制它打开新的框架。 我同意在没有登录位的框架之间切换,但不知道如何将登录功能附加到框架上

 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)
但是,未定义父级。您需要在
\uuuu init\uuuu
方法中保存
父项
,以便在此处访问它

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

lambda:parent.switch\u frame(PageOne)
替换为
parent.switch\u frame(PageOne)
刚才尝试的内容是:第51行,在“登录”中单击了“父项”。switch\u frame(PageOne)name错误:未定义名称“父项”错误:未定义名称“父项”“:您已将
parent
设置为类方法。在
\uuuuu init\uuuuuuuuuu
do
self.parent=parent
中,在
def\u login\u btn\u中单击(…
do
self.parent.switch(…