Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk # Think of this like the CSS for Tkinter
- # Defining constants
- LARGE_FONT = ("Century Gothic", 40)
- # The 'tk.Tk' inside the parenthesis is inheritance. Meaning that the class 'VATest' will inherit from 'tk.Tk'
- class VATest(tk.Tk):
- # This method will run everytime an instance of this class is initialized.
- # The 'self' parameter should always be passed through at the beginning. (Optional convention)
- # The '*args' and '**kwargs' are also both optional but conventionally included.
- # '*args' are used when passing through variables and '**kwargs' are used for dictionaries.
- def __init__(self, *args, **kwargs):
- # Initialize Tkinter
- tk.Tk.__init__(self, *args, **kwargs)
- # Set window title
- tk.Tk.wm_title(self, "Visual Acuity Assessment Tool")
- # Add container/window
- container = tk.Frame(self)
- # Putting elements inside the container
- container.pack(side="top", fill="both", expand=True)
- # First parameter is the minimum size. Weight is the priority.
- container.grid_rowconfigure(0, weight=1)
- container.grid_columnconfigure(0, weight=1)
- # Dictionary will contain the name of all frames.
- self.frames = {}
- for F in (StartPage, PageOne, PageTwo):
- frame = F(container, self)
- self.frames[F] = frame
- # Another option for arranging elements in a container. Pack or Grid
- # Sticky can be thought of as alignment and stretch.
- frame.grid(row=0, column=0, sticky="nsew")
- self.show_frame(StartPage)
- def show_frame(self, cont):
- # Select a user-defined frames inside the 'self.frames' dictionary
- # 'cont' serves as the controller to which frame is selected.
- frame = self.frames[cont]
- # Raise the selected frame from the dictionary.
- # I can use the 'tkraise' method because this class inherits from 'tk.Tk'.
- frame.tkraise()
- class StartPage(tk.Frame):
- # Initialize the frame
- def __init__(self, parent, controller):
- # Initialize 'tk.Frame'
- # THe 'parent' class is the 'VATest' class
- tk.Frame.__init__(self, parent)
- # Adding text to a tkinter window
- label = tk.Label(self, text="StartPage", font=LARGE_FONT)
- label.pack(pady=10, padx=10)
- # Use 'lambda' with the 'command' parameter to create a nameless function within a single line.
- gotoPageOne = ttk.Button(
- self,
- text="Visit Page 1",
- command=lambda: controller.show_frame(PageOne),
- )
- gotoPageOne.pack()
- gotoPageTwo = ttk.Button(
- self,
- text="Visit Page 2",
- command=lambda: controller.show_frame(PageTwo),
- )
- gotoPageTwo.pack()
- class PageOne(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- label = tk.Label(self, text="PageOne", font=LARGE_FONT)
- label.pack(pady=10, padx=10)
- goHome = ttk.Button(
- self,
- text="Back to StartPage",
- command=lambda: controller.show_frame(StartPage),
- )
- goHome.pack()
- gotoPageTwo = ttk.Button(
- self,
- text="Visit Page 2",
- command=lambda: controller.show_frame(PageTwo),
- )
- gotoPageTwo.pack()
- class PageTwo(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- label = tk.Label(self, text="PageTwo", font=LARGE_FONT)
- label.pack(pady=10, padx=10)
- goHome = ttk.Button(
- self,
- text="Back to StartPage",
- command=lambda: controller.show_frame(StartPage),
- )
- goHome.pack()
- gotoPageOne = ttk.Button(
- self,
- text="Visit Page 1",
- command=lambda: controller.show_frame(PageOne),
- )
- gotoPageOne.pack()
- app = VATest()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment