switch between frames in tkinter

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

showing results for - "switch between frames in tkinter"
Erica
14 Apr 2019
1try:
2    import tkinter as tk                # python 3
3    from tkinter import font as tkfont  # python 3
4except ImportError:
5    import Tkinter as tk     # python 2
6    import tkFont as tkfont  # python 2
7
8class SampleApp(tk.Tk):
9
10    def __init__(self, *args, **kwargs):
11        tk.Tk.__init__(self, *args, **kwargs)
12
13        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
14
15        # the container is where we'll stack a bunch of frames
16        # on top of each other, then the one we want visible
17        # will be raised above the others
18        container = tk.Frame(self)
19        container.pack(side="top", fill="both", expand=True)
20        container.grid_rowconfigure(0, weight=1)
21        container.grid_columnconfigure(0, weight=1)
22
23        self.frames = {}
24        for F in (StartPage, PageOne, PageTwo):
25            page_name = F.__name__
26            frame = F(parent=container, controller=self)
27            self.frames[page_name] = frame
28
29            # put all of the pages in the same location;
30            # the one on the top of the stacking order
31            # will be the one that is visible.
32            frame.grid(row=0, column=0, sticky="nsew")
33
34        self.show_frame("StartPage")
35
36    def show_frame(self, page_name):
37        '''Show a frame for the given page name'''
38        frame = self.frames[page_name]
39        frame.tkraise()
40
41
42class StartPage(tk.Frame):
43
44    def __init__(self, parent, controller):
45        tk.Frame.__init__(self, parent)
46        self.controller = controller
47        label = tk.Label(self, text="This is the start page", font=controller.title_font)
48        label.pack(side="top", fill="x", pady=10)
49
50        button1 = tk.Button(self, text="Go to Page One",
51                            command=lambda: controller.show_frame("PageOne"))
52        button2 = tk.Button(self, text="Go to Page Two",
53                            command=lambda: controller.show_frame("PageTwo"))
54        button1.pack()
55        button2.pack()
56
57
58class PageOne(tk.Frame):
59
60    def __init__(self, parent, controller):
61        tk.Frame.__init__(self, parent)
62        self.controller = controller
63        label = tk.Label(self, text="This is page 1", font=controller.title_font)
64        label.pack(side="top", fill="x", pady=10)
65        button = tk.Button(self, text="Go to the start page",
66                           command=lambda: controller.show_frame("StartPage"))
67        button.pack()
68
69
70class PageTwo(tk.Frame):
71
72    def __init__(self, parent, controller):
73        tk.Frame.__init__(self, parent)
74        self.controller = controller
75        label = tk.Label(self, text="This is page 2", font=controller.title_font)
76        label.pack(side="top", fill="x", pady=10)
77        button = tk.Button(self, text="Go to the start page",
78                           command=lambda: controller.show_frame("StartPage"))
79        button.pack()
80
81
82if __name__ == "__main__":
83    app = SampleApp()
84    app.mainloop()
85