Home >Backend Development >Python Tutorial >How to Add a Scrollbar to a Grid of Tkinter Labels?

How to Add a Scrollbar to a Grid of Tkinter Labels?

Susan Sarandon
Susan SarandonOriginal
2024-12-17 15:26:10738browse

How to Add a Scrollbar to a Grid of Tkinter Labels?

Adding a Scrollbar to a Group of Widgets in Tkinter

Overview

Tkinter provides scrollbars for widgets like List, Textbox, Canvas, and Entry. However, displaying a grid of label widgets with a scrollbar can be challenging since these widgets don't support scrollbars natively.

Solutions

1. Text Widget with window_create:

  • Create a text widget and add label widgets using the window_create method.
  • This method is simple but limits layout complexity.

2. Canvas with Embedded Frame:

  • Create a canvas widget with scrollbars attached to it.
  • Embed a frame within the canvas and place your label widgets inside the frame.
  • Determine the width and height of the frame and set the scrollregion option of the canvas to match these dimensions.

3. Direct Drawing on Canvas:

  • Create a custom class that inherits from tk.Frame to manage the scrollbar and canvas.
  • Place your label widgets within the embedded frame.
  • Bind events to adjust the scroll region based on the frame's configuration changes.

Object-Oriented Solution:

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)

        self.vsb.pack(side="right", fill="y")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")

        self.frame.bind("<Configure>", self.onFrameConfigure)

        self.populate()

    def populate(self):
        for row in range(100):
            tk.Label(self.frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0)
            t = "this is the second column for row %s" % row
            tk.Label(self.frame, text=t).grid(row=row, column=1)

    def onFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":
    root = tk.Tk()
    example = Example(root)
    example.pack(side="top", fill="both", expand=True)
    root.mainloop()

Procedural Solution:

import tkinter as tk

def populate(frame):
    for row in range(100):
        tk.Label(frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0)
        t = "this is the second column for row %s" % row
        tk.Label(frame, text=t).grid(row=row, column=1)

def onFrameConfigure(canvas):
    canvas.configure(scrollregion=canvas.bbox("all"))

root = tk.Tk()
canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
frame = tk.Frame(canvas, background="#ffffff")
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")

frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

populate(frame)

root.mainloop()

The above is the detailed content of How to Add a Scrollbar to a Grid of Tkinter Labels?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn