Advertisement
here2share

# Tk_frame_nav.py

May 23rd, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. # Tk_frame_nav.py
  2.  
  3. import Tkinter as tk     # python 2
  4. import tkFont as tkfont  # python 2
  5.  
  6. class SampleApp(tk.Tk):
  7.  
  8.     def __init__(self, *args, **kwargs):
  9.         tk.Tk.__init__(self, *args, **kwargs)
  10.  
  11.         self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
  12.  
  13.         # the container is where we'll stack a bunch of frames
  14.         # on top of each other, then the one we want visible
  15.         # will be raised above the others
  16.         container = tk.Frame(self)
  17.         container.pack(side="top", fill="both", expand=True)
  18.         container.grid_rowconfigure(0, weight=1)
  19.         container.grid_columnconfigure(0, weight=1)
  20.  
  21.         self.frames = {}
  22.         for F in (StartPage, PageOne, PageTwo, PageThree):
  23.             page_name = F.__name__
  24.             frame = F(parent=container, controller=self)
  25.             self.frames[page_name] = frame
  26.  
  27.             # put all of the pages in the same location;
  28.             # the one on the top of the stacking order
  29.             # will be the one that is visible.
  30.             frame.grid(row=0, column=0, sticky="nsew")
  31.  
  32.         self.show_frame("StartPage")
  33.  
  34.     def show_frame(self, page_name):
  35.         '''Show a frame for the given page name'''
  36.         frame = self.frames[page_name]
  37.         frame.tkraise()
  38.  
  39.  
  40. class StartPage(tk.Frame):
  41.  
  42.     def __init__(self, parent, controller):
  43.         tk.Frame.__init__(self, parent)
  44.         self.controller = controller
  45.         label = tk.Label(self, text="This is the start page", font=controller.title_font)
  46.         label.pack(side="top", fill="x", padx=10, pady=10)
  47.  
  48.         button1 = tk.Button(self, text="Go to Page 1",
  49.                             command=lambda: controller.show_frame("PageOne"))
  50.         button2 = tk.Button(self, text="Go to Page 2",
  51.                             command=lambda: controller.show_frame("PageTwo"))
  52.         button3 = tk.Button(self, text="Go to Page 3",
  53.                             command=lambda: controller.show_frame("PageThree"))
  54.         button1.pack()
  55.         button2.pack()
  56.         button3.pack()
  57.         ypad = tk.Label(self, text="")
  58.         ypad.pack(side="top", fill="x")
  59.  
  60.  
  61. class PageOne(tk.Frame):
  62.  
  63.     def __init__(self, parent, controller):
  64.         tk.Frame.__init__(self, parent)
  65.         self.controller = controller
  66.         label = tk.Label(self, text="This is page 1", font=controller.title_font)
  67.         label.pack(side="top", fill="x", pady=10)
  68.         button = tk.Button(self, text="Go to the start page",
  69.                            command=lambda: controller.show_frame("StartPage"))
  70.         button.pack()
  71.  
  72.  
  73. class PageTwo(tk.Frame):
  74.  
  75.     def __init__(self, parent, controller):
  76.         tk.Frame.__init__(self, parent)
  77.         self.controller = controller
  78.         label = tk.Label(self, text="This is page 2", font=controller.title_font)
  79.         label.pack(side="top", fill="x", pady=10)
  80.         button = tk.Button(self, text="Go to the start page",
  81.                            command=lambda: controller.show_frame("StartPage"))
  82.         button.pack()
  83.  
  84.  
  85. class PageThree(tk.Frame):
  86.  
  87.     def __init__(self, parent, controller):
  88.         tk.Frame.__init__(self, parent)
  89.         self.controller = controller
  90.         label = tk.Label(self, text="This is page 3", font=controller.title_font)
  91.         label.pack(side="top", fill="x", pady=10)
  92.         button1 = tk.Button(self, text="Go to page 1",
  93.                            command=lambda: controller.show_frame("PageOne"))
  94.         button2 = tk.Button(self, text="Go to page 2",
  95.                            command=lambda: controller.show_frame("PageTwo"))
  96.         button1.pack()
  97.         button2.pack()
  98.  
  99.  
  100. if __name__ == "__main__":
  101.     app = SampleApp()
  102.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement