tkinter frame switch

Solutions on MaxInterview for tkinter frame switch by the best coders in the world

showing results for - "tkinter frame switch"
Julian
28 Jun 2019
1 pythonCopytry:
2    import Tkinter as tk
3except:
4    import tkinter as tk
5    
6class SampleApp(tk.Tk):
7    def __init__(self):
8        tk.Tk.__init__(self)
9        self._frame = None
10        self.switch_frame(StartPage)
11
12    def switch_frame(self, frame_class):
13        new_frame = frame_class(self)
14        if self._frame is not None:
15            self._frame.destroy()
16        self._frame = new_frame
17        self._frame.pack()
18
19class StartPage(tk.Frame):
20    def __init__(self, master):
21        tk.Frame.__init__(self, master)
22        tk.Label(self, text="Start page", font=('Helvetica', 18, "bold")).pack(side="top", fill="x", pady=5)
23        tk.Button(self, text="Go to page one",
24                  command=lambda: master.switch_frame(PageOne)).pack()
25        tk.Button(self, text="Go to page two",
26                  command=lambda: master.switch_frame(PageTwo)).pack()
27
28class PageOne(tk.Frame):
29    def __init__(self, master):
30        tk.Frame.__init__(self, master)
31        tk.Frame.configure(self,bg='blue')
32        tk.Label(self, text="Page one", font=('Helvetica', 18, "bold")).pack(side="top", fill="x", pady=5)
33        tk.Button(self, text="Go back to start page",
34                  command=lambda: master.switch_frame(StartPage)).pack()
35
36class PageTwo(tk.Frame):
37    def __init__(self, master):
38        tk.Frame.__init__(self, master)
39        tk.Frame.configure(self,bg='red')
40        tk.Label(self, text="Page two", font=('Helvetica', 18, "bold")).pack(side="top", fill="x", pady=5)
41        tk.Button(self, text="Go back to start page",
42                  command=lambda: master.switch_frame(StartPage)).pack()
43
44if __name__ == "__main__":
45    app = SampleApp()
46    app.mainloop()
47
similar questions
queries leading to this page
tkinter frame switch