tkinter scroll frame in python

To create a scrollable frame in tkinter in Python, you can utilize the Canvas widget along with a Scrollbar. Here's a simple example:

main.py
import tkinter as tk

root = tk.Tk()
root.title("Scrollable Frame Example")

canvas = tk.Canvas(root)
frame = tk.Frame(canvas)
scrollbar = tk.Scrollbar(root, orient="vertical", command=canvas.yview)

scrollable_frame = tk.Frame(canvas)

scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)

canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)

for i in range(50):
    tk.Label(scrollable_frame, text=f"Label {i}").pack()

canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
643 chars
27 lines

This code creates a scrollable frame containing 50 labels within a canvas with a scrollbar. You can adjust the number of labels or customize the frame as needed.

related categories

gistlibby LogSnag