Skip to content

Instantly share code, notes, and snippets.

@jgram925
Last active September 30, 2022 02:42
Show Gist options
  • Save jgram925/8d1fd306f80e9c4b4bb557b0910c0e14 to your computer and use it in GitHub Desktop.
Save jgram925/8d1fd306f80e9c4b4bb557b0910c0e14 to your computer and use it in GitHub Desktop.
Multiple Pages on Tkinter.md

This is an example from StackOverflow (link provided below), from the user Bryan Oakley. This not a solution provided by myself. Thank you Bryan for providing this useful info! StackOverflow Link

import tkinter as tk

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 1")
       label.pack(side="top", fill="both", expand=True)

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.pack(side="top", fill="both", expand=True)

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)        

        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)        

        b1.pack(side="left")
        b2.pack(side="left")        

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")
    root.mainloop()
@neroist
Copy link

neroist commented Feb 11, 2021

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment