Cover Image for Tkinter Application to Switch Between Different Page Frames in Python
645 views

Tkinter Application to Switch Between Different Page Frames in Python

To create a Tkinter application that switches between different page frames or views, you can use the tkinter library to build the graphical user interface and manage the display of different frames. Here’s an example of how to do this:

Python
import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # Create a container to hold the frames
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # Create a dictionary to store different page frames
        self.frames = {}

        # Define the pages and add them to the frames dictionary
        for PageClass in (StartPage, PageOne, PageTwo):
            page_name = PageClass.__name__
            frame = PageClass(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        # Show the initial page
        self.show_frame("StartPage")

    def show_frame(self, page_name):
        # Show the requested page by raising it to the top
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Welcome to the Start Page")
        label.pack(pady=10, padx=10)
        button1 = tk.Button(self, text="Go to Page One", command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two", command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is Page One")
        label.pack(pady=10, padx=10)
        button = tk.Button(self, text="Go to Start Page", command=lambda: controller.show_frame("StartPage"))
        button.pack()

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is Page Two")
        label.pack(pady=10, padx=10)
        button = tk.Button(self, text="Go to Start Page", command=lambda: controller.show_frame("StartPage"))
        button.pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

In this code:

  • We define a SampleApp class that inherits from tk.Tk. This class serves as the main application and manages the different page frames.
  • We create a container frame to hold the different page frames. Each page frame is created as a separate class (StartPage, PageOne, and PageTwo) that inherits from tk.Frame.
  • The self.frames dictionary is used to store references to the different page frames.
  • In the show_frame method, we use the tkraise method to raise the requested frame to the top, making it visible.
  • Each page frame contains buttons that allow the user to navigate between different pages.
  • The application is started by creating an instance of SampleApp and calling its mainloop method.

You can expand upon this basic example to create a more complex application with additional pages and functionality as needed.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS