切换框架时形成错误

时间:2018-12-15 14:06:16

标签: python user-interface tkinter frames

我正在为我的聊天应用程序设计一个登录屏幕。由于我没有太多有关切换帧的信息,因此我正在查看代码here并相应地为我的项目进行编辑。现在,我希望在正确输入凭据后显示“第2页”。但是,当我正确输入昵称和密码时,昵称表格仍保留在第二个屏幕中,看起来就像我上载的屏幕快照一样混乱。 Screenshot

如何解决此问题?代码如下:

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
        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 (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            F.grid_propagate(self)


            frame.grid(row=0, column=0, sticky="nsew")

       self.show_frame("StartPage")

   def show_frame(self, page_name):
       frame = self.frames[page_name]
       frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, width=500, height=500)
        self.controller = controller

        db = MySQLdb.connect(host="localhost", user="root", passwd="", db="members")
        cursor = db.cursor()

        label = tk.Label(self, text="Nickname:")
        label.place(x=140, y=150)


        user_id = tk.Entry()
        user_id.place(x=210, y=150)


        label_2 = tk.Label(self, text="Password:")
        label_2.place(x=140, y=200)

        password = tk.Entry(self, show="*")
        password.place(x=210, y=200)

        def auth_login(event=None):
            id = user_id.get()
            pswd = password.get()
            cursor.execute(
            "SELECT nickname, password FROM chatmembers WHERE nickname='{}' AND password='{}' ".format(id, pswd))
            if cursor.fetchone():
                controller.show_frame("PageTwo")

            else:
                messagebox.showerror("Wrong credentials", "Either nickname or password is wrong. Please try again.")

           login_button = tk.Button(self, text="Log In", command=auth_login, height=2, width=20)
           login_button.place(x=195, y=250)



class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, width=500, height=500)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                       command=lambda: controller.show_frame("StartPage"))
        button.place(x=100, y=200)




class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                       command=lambda: controller.show_frame("StartPage"))
        button.pack()


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

0 个答案:

没有答案