Guest User

Code from Tutorial

a guest
Jan 29th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk  # Think of this like the CSS for Tkinter
  3.  
  4. # Defining constants
  5. LARGE_FONT = ("Century Gothic", 40)
  6.  
  7. # The 'tk.Tk' inside the parenthesis is inheritance. Meaning that the class 'VATest' will inherit from 'tk.Tk'
  8. class VATest(tk.Tk):
  9.  
  10.     # This method will run everytime an instance of this class is initialized.
  11.     # The 'self' parameter should always be passed through at the beginning. (Optional convention)
  12.     # The '*args' and '**kwargs' are also both optional but conventionally included.
  13.     # '*args' are used when passing through variables and '**kwargs' are used for dictionaries.
  14.     def __init__(self, *args, **kwargs):
  15.  
  16.         # Initialize Tkinter
  17.         tk.Tk.__init__(self, *args, **kwargs)
  18.  
  19.         # Set window title
  20.         tk.Tk.wm_title(self, "Visual Acuity Assessment Tool")
  21.  
  22.         # Add container/window
  23.         container = tk.Frame(self)
  24.  
  25.         # Putting elements inside the container
  26.         container.pack(side="top", fill="both", expand=True)
  27.  
  28.         # First parameter is the minimum size. Weight is the priority.
  29.         container.grid_rowconfigure(0, weight=1)
  30.         container.grid_columnconfigure(0, weight=1)
  31.  
  32.         # Dictionary will contain the name of all frames.
  33.         self.frames = {}
  34.  
  35.         for F in (StartPage, PageOne, PageTwo):
  36.             frame = F(container, self)
  37.  
  38.             self.frames[F] = frame
  39.  
  40.             # Another option for arranging elements in a container. Pack or Grid
  41.             # Sticky can be thought of as alignment and stretch.
  42.             frame.grid(row=0, column=0, sticky="nsew")
  43.  
  44.         self.show_frame(StartPage)
  45.  
  46.     def show_frame(self, cont):
  47.  
  48.         # Select a user-defined frames inside the 'self.frames' dictionary
  49.         # 'cont' serves as the controller to which frame is selected.
  50.         frame = self.frames[cont]
  51.  
  52.         # Raise the selected frame from the dictionary.
  53.         # I can use the 'tkraise' method because this class inherits from 'tk.Tk'.
  54.         frame.tkraise()
  55.  
  56.  
  57. class StartPage(tk.Frame):
  58.  
  59.     # Initialize the frame
  60.     def __init__(self, parent, controller):
  61.  
  62.         # Initialize 'tk.Frame'
  63.         # THe 'parent' class is the 'VATest' class
  64.         tk.Frame.__init__(self, parent)
  65.  
  66.         # Adding text to a tkinter window
  67.         label = tk.Label(self, text="StartPage", font=LARGE_FONT)
  68.         label.pack(pady=10, padx=10)
  69.  
  70.         # Use 'lambda' with the 'command' parameter to create a nameless function within a single line.
  71.         gotoPageOne = ttk.Button(
  72.             self,
  73.             text="Visit Page 1",
  74.             command=lambda: controller.show_frame(PageOne),
  75.         )
  76.         gotoPageOne.pack()
  77.  
  78.         gotoPageTwo = ttk.Button(
  79.             self,
  80.             text="Visit Page 2",
  81.             command=lambda: controller.show_frame(PageTwo),
  82.         )
  83.         gotoPageTwo.pack()
  84.  
  85.  
  86. class PageOne(tk.Frame):
  87.     def __init__(self, parent, controller):
  88.         tk.Frame.__init__(self, parent)
  89.  
  90.         label = tk.Label(self, text="PageOne", font=LARGE_FONT)
  91.         label.pack(pady=10, padx=10)
  92.  
  93.         goHome = ttk.Button(
  94.             self,
  95.             text="Back to StartPage",
  96.             command=lambda: controller.show_frame(StartPage),
  97.         )
  98.         goHome.pack()
  99.  
  100.         gotoPageTwo = ttk.Button(
  101.             self,
  102.             text="Visit Page 2",
  103.             command=lambda: controller.show_frame(PageTwo),
  104.         )
  105.         gotoPageTwo.pack()
  106.  
  107.  
  108. class PageTwo(tk.Frame):
  109.     def __init__(self, parent, controller):
  110.         tk.Frame.__init__(self, parent)
  111.  
  112.         label = tk.Label(self, text="PageTwo", font=LARGE_FONT)
  113.         label.pack(pady=10, padx=10)
  114.  
  115.         goHome = ttk.Button(
  116.             self,
  117.             text="Back to StartPage",
  118.             command=lambda: controller.show_frame(StartPage),
  119.         )
  120.         goHome.pack()
  121.  
  122.         gotoPageOne = ttk.Button(
  123.             self,
  124.             text="Visit Page 1",
  125.             command=lambda: controller.show_frame(PageOne),
  126.         )
  127.         gotoPageOne.pack()
  128.  
  129.  
  130. app = VATest()
  131. app.mainloop()
  132.  
Advertisement
Add Comment
Please, Sign In to add comment