[Python / Tkinter] A class that creates a scrollable Frame

Introduction

Tkinter is the easiest choice for creating GUIs in Python. However, there are some dissatisfactions with the content. One of them is that you can't scroll the Frame. The following introduces a class for creating a scrollable Frame.

The classes introduced below are some of the classes posted on the following sites. Thank you very much for helping me: ・ Scrollable Frames in Tkinter | The Teclado Blog, https://blog.tecladocode.com/tkinter-scrollable-frames/

Full code

Since it is not possible to attach a Scrollbar to a Frame in Tkinter, we first create a Canvas that is a scrollable widget and put the Frame in it. And the width of Frame is included in the scrollable range of Canvas. This makes it possible to scroll the internal Frame at the same time as the Canvas is scrolled.

import tkinter as tk
import tkinter.ttk as ttk

class ScrollableFrame(ttk.Frame):
    def __init__(self, container, bar_x = True, bar_y = True):
        super().__init__(container)
        self.canvas = tk.Canvas(self)
        self.scrollable_frame = ttk.Frame(self.canvas)
        self.scrollable_frame.bind(
            "<Configure>",
            lambda e: self.canvas.configure(
                scrollregion=self.canvas.bbox("all")
            )
        )
        self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
        if bar_y:
            self.scrollbar_y = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
            self.scrollbar_y.pack(side=tk.RIGHT, fill="y")
            self.canvas.configure(yscrollcommand=self.scrollbar_y.set)
        if bar_x:
            self.scrollbar_x = ttk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
            self.scrollbar_x.pack(side=tk.BOTTOM, fill="x")
            self.canvas.configure(xscrollcommand=self.scrollbar_x.set)
        self.canvas.pack(side=tk.LEFT, fill="both", expand=True)

Actually use

When you actually use it, it will be as follows.

root = tk.Tk()

frame = ScrollableFrame(root)

for i in range(50):
    for j in range(50):
        ttk.Entry(frame.scrollable_frame, width=5).grid(row=i, column=j)

frame.pack()
root.mainloop()

2020-05-08_06h56_38.png

How to use

There are two points, when the instance is created and the widget is placed in the ScrollableFrame.

① When creating an instance

Scrollable frames may be rare if you need both the x-axis and the y-axis. Therefore, I made it possible to select the Scrollbar to add. The initial value is True, so select Hide if necessary.

def __init__(self, container, bar_x = True, bar_y = True):

② Placement of Widget in ScrollableFrame

As explained above, the Frame is created in the Canvas. Therefore, when placing the widget, it is necessary to place it in the Frame (scrollable_frame) in the Canvas. Do as follows.

frame = ScrollableFrame(root)
ttk.Label(frame.scrollable_frame, text="sample").pack

[Supplement]

This class is a modification of the class posted on the following site. ・ Scrollable Frames in Tkinter | The Teclado Blog, https://blog.tecladocode.com/tkinter-scrollable-frames/

There are two changes:

--Since Scrollbar was only available on the y-axis, we made changes to support both the y-axis and the x-axis. ――It is often more convenient to be able to select display / non-display of the x-axis and y-axis, so we have made it possible to select that as well.

Recommended Posts

[Python / Tkinter] A class that creates a scrollable Frame
[Python] A program that creates stairs with #
Create a frame with transparent background with tkinter [Python]
Python: Create a class that supports unpacked assignment
A class that creates DB creation-data insertion with SQLite3 quickly
[Python] A program that creates a two-dimensional array by combining integers
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
How to write a Python class
Create a python GUI using tkinter
[Python] Inherit a class with class variables
A class that summarizes frequently used methods in twitter api (python)
[python] I made a class that can write a file tree quickly
Generate a first class collection in Python
A class that hits the DMM API
[Python] How to make a class iterable
[Python] Create a LineBot that runs regularly
Generate a class from a string in Python
A typed world that begins with Python
A program that plays rock-paper-scissors using Python
PGM that automatically creates a walking route
[Python] A program that rounds the score
I tried to create a class that can easily serialize Json in Python
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
[Python] A tool that allows intuitive relative import
A nice nimporter that connects nim and python
Python variadic memorandum when inheriting a defined class
I wrote a class in Python3 and Java
Create a page that loads infinitely with python
A program that removes duplicate statements in Python
[Note] Create a one-line timezone class with python
[Python] I made a Youtube Downloader with Tkinter.
Publish a Python module that calculates meteorological factors
Python: Prepare a serializer for the class instance:
"Python Kit" that calls a Python script from Swift
A Vim plugin that automatically formats Python styles
[Python] class, instance
"Kanrika" python class
About python, class
Python class, instance
#Python basics (class)
[Python] Tkinter template
Create a fake class that also cheats is instance
[Python] A program that counts the number of valleys
A simple Python HTTP server that supports Range Requests
I made a VM that runs OpenCV for Python
A python implementation of the Bayesian linear regression class
Python that merges a lot of excel into one excel
From a book that programmers can learn ... (Python): Pointer
What's in that variable (when running a Python script)
Created a Python library DateTimeRange that handles time ranges
In Python, create a decorator that dynamically accepts arguments Create a decorator
[Python] tkinter Code that is likely to be reused
[Python] How to write a docstring that conforms to PEP8
A server that echoes data POSTed with flask / python
A function that easily calculates a listwise removal tree (Python)
[Python] A convenient library that converts kanji to hiragana
A memo that I touched the Datastore with python
[Python] Create a Tkinter program distribution file with cx_Freeze
[Python] A program that compares the positions of kangaroos.
A Python program that converts ical data into text
MALSS, a tool that supports machine learning in Python