Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a scrollable frame in Tkinter involves using a Canvas
widget and a scrollbar together. Here's a step-by-step tutorial on how to set up a scrollable frame in Tkinter:
Setting up the main window
Start with a simple window:
from tkinter import Tk, Canvas, Frame, Scrollbar, VERTICAL, NW root = Tk() root.title("Scrollable Frame in Tkinter") root.geometry("300x300") root.mainloop()
Creating the Scrollable Frame
Here's how you can add a scrollable frame to the window:
from tkinter import Tk, Canvas, Frame, Scrollbar, VERTICAL, NW class ScrollableFrame(Frame): def __init__(self, master, **kwargs): Frame.__init__(self, master, **kwargs) # Create a canvas inside the frame self.canvas = Canvas(self) self.canvas.grid(row=0, column=0, sticky="nsew") # Add a vertical scrollbar to the frame self.scrollbar = Scrollbar(self, orient=VERTICAL, command=self.canvas.yview) self.scrollbar.grid(row=0, column=1, sticky="ns") # Configure canvas to work with scrollbar self.canvas.configure(yscrollcommand=self.scrollbar.set) # Add another frame inside the canvas for the actual content self.inner_frame = Frame(self.canvas) self.canvas.create_window((0, 0), window=self.inner_frame, anchor=NW) # Ensure scrolling works self.inner_frame.bind("<Configure>", lambda event: self.canvas.configure(scrollregion=self.canvas.bbox("all"))) root = Tk() root.title("Scrollable Frame in Tkinter") root.geometry("300x300") frame = ScrollableFrame(root) frame.pack(fill="both", expand=True) # Add some sample content for i in range(50): label = Label(frame.inner_frame, text=f"Label {i}") label.pack() root.mainloop()
Here's what's happening in the code:
ScrollableFrame
class that inherits from the standard Frame
.ScrollableFrame
, we've added a Canvas
widget, which is the main widget responsible for the scrolling behavior.Scrollbar
and linked it to the canvas's vertical scrolling.Frame
(referred to as inner_frame
), which will contain the actual content.inner_frame.bind
method ensures that the scroll region of the canvas is updated whenever the size of the inner_frame
changes (like when adding more widgets).This setup creates a scrollable frame that you can use as a drop-in replacement for a regular frame whenever you need scrolling capabilities.
Creating a scrollable frame involves using a Canvas
widget and a Frame
widget inside it. The Canvas
provides scrolling functionality.
import tkinter as tk from tkinter import ttk class ScrollableFrame(tk.Frame): def __init__(self, container, *args, **kwargs): super().__init__(container, *args, **kwargs) # Create a canvas with a vertical scrollbar canvas = tk.Canvas(self) scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) self.scrollable_frame = scrollable_frame # Example usage root = tk.Tk() root.title("Scrollable Frame Example") scrollable_frame = ScrollableFrame(root) scrollable_frame.pack(fill="both", expand=True) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame.scrollable_frame, text=f"Label {i}").pack() root.mainloop()
A scrollable frame can be created by using a Canvas
widget and a Frame
widget inside it. The Canvas
is configured with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollable Frame Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
To add a scrollbar to a frame, you can use the Canvas
widget and configure it with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollbar for Frame Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
To add a vertical scrollbar within a frame, use the Canvas
widget and configure it with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Vertical Scrollbar in Frame Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
Make a frame scrollable in Tkinter by embedding it inside a Canvas
widget and configuring the Canvas
with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Make Frame Scrollable Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
To create a scrollable canvas within a frame, embed the canvas inside a Canvas
widget and configure the Canvas
with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollable Canvas in Frame Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
Adding a scrollbar to the content of a frame involves configuring a Canvas
with a vertical scrollbar and embedding the frame inside it.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollbar to Frame Content Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
To create a scrollable window with a frame, embed the frame inside a Canvas
widget and configure the Canvas
with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollable Window with Frame Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
Create a scrollable frame widget by embedding a frame inside a Canvas
widget and configuring the Canvas
with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollable Frame Widget Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()
Create a scrollable area within a frame by embedding the frame inside a Canvas
widget and configuring the Canvas
with a vertical scrollbar.
# ... (previous code) # Example usage root = tk.Tk() root.title("Scrollable Area within Frame Example") # Create a canvas with a vertical scrollbar canvas = tk.Canvas(root) scrollbar = ttk.Scrollbar(root, orient="vertical", command=canvas.yview) scrollable_frame = ttk.Frame(canvas) # Configure the canvas canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") canvas.configure(yscrollcommand=scrollbar.set) # Pack the components canvas.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y") # Bind canvas resize to configure the scrollable region canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all"))) # Add widgets to the scrollable frame for i in range(50): ttk.Label(scrollable_frame, text=f"Label {i}").pack() root.mainloop()