Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to switch between two frames in Tkinter?
In GUI applications, you often need multiple screens to organize different sections of your program. Tkinter allows you to create separate Frame widgets that can be switched dynamically to show different content within the same window.
A Frame widget acts as a container to group related widgets together. By creating multiple frames and controlling their visibility, you can implement screen navigation in your Tkinter application.
Basic Frame Switching Example
Let's create two frames ? a greeting frame and an order frame ? with buttons to switch between them ?
# Import the required libraries
from tkinter import *
from tkinter import font
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Create two frames in the window
greet = Frame(win)
order = Frame(win)
# Define a function for switching the frames
def change_to_greet():
greet.pack(fill='both', expand=1)
order.pack_forget()
def change_to_order():
order.pack(fill='both', expand=1)
greet.pack_forget()
# Create fonts for making difference in the frame
font1 = font.Font(family='Georgia', size='22', weight='bold')
font2 = font.Font(family='Arial', size='12')
# Add a heading label in the frames
label1 = Label(greet, text="Hey There! Welcome to TutorialsPoint.", foreground="green3", font=font1)
label1.pack(pady=20)
label2 = Label(order, text="Find all the interesting Tutorials.", foreground="blue", font=font2)
label2.pack(pady=20)
# Add buttons to switch between two frames
btn1 = Button(win, text="Switch to Order", font=font2, command=change_to_order)
btn1.pack(pady=10)
btn2 = Button(win, text="Switch to Greet", font=font2, command=change_to_greet)
btn2.pack(pady=10)
# Initially show the greet frame
change_to_greet()
win.mainloop()
How It Works
The frame switching mechanism uses two key methods ?
-
pack()? Makes the frame visible and positions it in the window -
pack_forget()? Hides the frame from view without destroying it
When switching frames, we pack the target frame while hiding the current one using pack_forget().
Enhanced Frame Switching with Class-Based Approach
For better organization, you can create a class-based frame switcher ?
import tkinter as tk
from tkinter import ttk
class FrameSwitcher:
def __init__(self, root):
self.root = root
self.root.title("Frame Switcher")
self.root.geometry("500x300")
# Create container frame
self.container = tk.Frame(root)
self.container.pack(fill="both", expand=True)
# Create frames dictionary
self.frames = {}
# Create different frames
for frame_class in (HomePage, SettingsPage):
frame = frame_class(self.container, self)
self.frames[frame_class] = frame
frame.grid(row=0, column=0, sticky="nsew")
# Show initial frame
self.show_frame(HomePage)
def show_frame(self, frame_class):
frame = self.frames[frame_class]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Home Page", font=("Arial", 16))
label.pack(pady=20)
button = tk.Button(self, text="Go to Settings",
command=lambda: controller.show_frame(SettingsPage))
button.pack()
class SettingsPage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Settings Page", font=("Arial", 16))
label.pack(pady=20)
button = tk.Button(self, text="Go to Home",
command=lambda: controller.show_frame(HomePage))
button.pack()
# Create and run application
root = tk.Tk()
app = FrameSwitcher(root)
root.mainloop()
Key Methods for Frame Management
| Method | Purpose | Use Case |
|---|---|---|
pack() |
Show frame | Simple frame switching |
pack_forget() |
Hide frame | Remove from layout |
grid() |
Position frame | Complex layouts |
tkraise() |
Bring to front | Overlapping frames |
Conclusion
Frame switching in Tkinter can be achieved using pack() and pack_forget() methods for simple cases, or tkraise() for overlapping frames. The class-based approach provides better organization for complex multi-screen applications.
