python tkinter pages in one window

Solutions on MaxInterview for python tkinter pages in one window by the best coders in the world

showing results for - "python tkinter pages in one window"
Samuel
11 Feb 2016
1import tkinter as tk
2
3
4LARGE_FONT= ("Verdana", 12)
5
6
7class SeaofBTCapp(tk.Tk):
8
9    def __init__(self, *args, **kwargs):
10        
11        tk.Tk.__init__(self, *args, **kwargs)
12        container = tk.Frame(self)
13
14        container.pack(side="top", fill="both", expand = True)
15
16        container.grid_rowconfigure(0, weight=1)
17        container.grid_columnconfigure(0, weight=1)
18
19        self.frames = {}
20
21        for F in (StartPage, PageOne, PageTwo):
22
23            frame = F(container, self)
24
25            self.frames[F] = frame
26
27            frame.grid(row=0, column=0, sticky="nsew")
28
29        self.show_frame(StartPage)
30
31    def show_frame(self, cont):
32
33        frame = self.frames[cont]
34        frame.tkraise()
35
36        
37class StartPage(tk.Frame):
38
39    def __init__(self, parent, controller):
40        tk.Frame.__init__(self,parent)
41        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
42        label.pack(pady=10,padx=10)
43
44        button = tk.Button(self, text="Visit Page 1",
45                            command=lambda: controller.show_frame(PageOne))
46        button.pack()
47
48        button2 = tk.Button(self, text="Visit Page 2",
49                            command=lambda: controller.show_frame(PageTwo))
50        button2.pack()
51
52
53class PageOne(tk.Frame):
54
55    def __init__(self, parent, controller):
56        tk.Frame.__init__(self, parent)
57        label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
58        label.pack(pady=10,padx=10)
59
60        button1 = tk.Button(self, text="Back to Home",
61                            command=lambda: controller.show_frame(StartPage))
62        button1.pack()
63
64        button2 = tk.Button(self, text="Page Two",
65                            command=lambda: controller.show_frame(PageTwo))
66        button2.pack()
67
68
69class PageTwo(tk.Frame):
70
71    def __init__(self, parent, controller):
72        tk.Frame.__init__(self, parent)
73        label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
74        label.pack(pady=10,padx=10)
75
76        button1 = tk.Button(self, text="Back to Home",
77                            command=lambda: controller.show_frame(StartPage))
78        button1.pack()
79
80        button2 = tk.Button(self, text="Page One",
81                            command=lambda: controller.show_frame(PageOne))
82        button2.pack()
83        
84
85
86app = SeaofBTCapp()
87app.mainloop()