97

I am using Python to parse entries from a log file, and display the entry contents using Tkinter and so far it's been excellent. The output is a grid of label widgets, but sometimes there are more rows than can be displayed on the screen. I'd like to add a scrollbar, which looks like it should be very easy, but I can't figure it out.

The documentation implies that only the List, Textbox, Canvas and Entry widgets support the scrollbar interface. None of these appear to be suitable for displaying a grid of widgets. It's possible to put arbitrary widgets in a Canvas widget, but you appear to have to use absolute co-ordinates, so I wouldn't be able to use the grid layout manager?

I've tried putting the widget grid into a Frame, but that doesn't seem to support the scrollbar interface, so this doesn't work:

mainframe = Frame(root, yscrollcommand=scrollbar.set)

Can anyone suggest a way round this limitation? I'd hate to have to rewrite in PyQt and increase my executable image size by so much, just to add a scrollbar!

stovfl
  • 14,998
  • 7
  • 24
  • 51
Simon Hibbs
  • 5,941
  • 5
  • 26
  • 32

3 Answers3

174

Overview

You can only associate scrollbars with a few widgets, and the root widget and Frame aren't part of that group of widgets.

There are at least a couple of ways to do this. If you need a simple vertical or horizontal group of widgets, you can use a text widget and the window_create method to add widgets. This method is simple, but doesn't allow for a complex layout of the widgets.

A more common general-purpose solution is to create a canvas widget and associate the scrollbars with that widget. Then, into that canvas embed the frame that contains your label widgets. Determine the width/height of the frame and feed that into the canvas scrollregion option so that the scrollregion exactly matches the size of the frame.

Why put the widgets in a frame rather than directly in the canvas? A scrollbar attached to a canvas can only scroll items created with one of the create_ methods. You cannot scroll items added to a canvas with pack, place, or grid. By using a frame, you can use those methods inside the frame, and then call create_window once for the frame.

Drawing the text items directly on the canvas isn't very hard, so you might want to reconsider that approach if the frame-embedded-in-a-canvas solution seems too complex. Since you're creating a grid, the coordinates of each text item is going to be very easy to compute, especially if each row is the same height (which it probably is if you're using a single font).

For drawing directly on the canvas, just figure out the line height of the font you're using (and there are commands for that). Then, each y coordinate is row*(lineheight+spacing). The x coordinate will be a fixed number based on the widest item in each column. If you give everything a tag for the column it is in, you can adjust the x coordinate and width of all items in a column with a single command.

Object-oriented solution

Here's an example of the frame-embedded-in-canvas solution, using an object-oriented approach:

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):
        '''Put in some fake data'''
        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):
        '''Reset the scroll region to encompass the inner frame'''
        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

Here is a solution that doesn't use a class:

import tkinter as tk

def populate(frame):
    '''Put in some fake data'''
    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):
    '''Reset the scroll region to encompass the inner frame'''
    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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    I'm trying this now. To start I'm just loading the data into a frame then putting the frame in a canvas, but the window isn't sizing to the canvas, and the grid options to determine the frame geometry aren't working. I'll post an update if I make any progress. – Simon Hibbs Jun 23 '10 at 14:08
  • 2
    @Simon Hibbs: I added an example that illustrates how to do it. – Bryan Oakley Jun 23 '10 at 15:59
  • That's excellent, thanks. I've got it working nicely. What's the intent of the width and height variables in OnFrameConfigure though? They aren't currently used and the application window isn't being set to the bounds of the canvas, which I'd expect. That's just finessing things though, thanks for your help. – Simon Hibbs Jun 23 '10 at 17:06
  • @Simon Hibbs: width and height was mistakenly left in. I'll remove them. – Bryan Oakley Jun 23 '10 at 18:04
  • Is there an easy way to enable sensible horizontal resizing on this? If I resize the width of the window of your example the width of the frame stays the same. How would I implement it if I wanted to scale (for example) column 1 accordingly? – Dani Gehtdichnixan May 13 '13 at 13:52
  • 1
    @DaniGehtdichnixan: you can create a binding on the canvas for the `` event, which will fire whenever the canvas is resized. In the event handler you can adjust the `minsize` attribute of one of the columns so that it fills the whole canvas (eg: `self.frame.columnconfigure(1, minsize=SIZE)`, where you do a little math to calculate SIZE). – Bryan Oakley May 13 '13 at 17:37
  • I'm using this code for different `FrameLabels` and I'm having problems adding the Scrollbar to each `FrameLabel`. Any help would be much appreciated: http://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-grid-of-widgets-in-tkinter – Avión Jan 29 '14 at 12:04
  • @Borja: when you say "having trouble...", that helps no one. It's impossible for me to guess why you are having trouble, or what sort of trouble you are having. If you have a question, create a new question. – Bryan Oakley Jan 29 '14 at 12:10
  • My fault, sorry Bryan. I pasted this question. Here's the real URL for my question: http://stackoverflow.com/questions/21429014/tkinter-add-scrollbar-for-each-labelframe – Avión Jan 29 '14 at 12:12
  • my scroll-wheel does not work on that. python3 on windows7. investigating why.... though not having a scroll helper for even a label, i might move to another framework... – gcb Jul 01 '15 at 23:12
  • @gcb: scrollwheels can be made to work. There are a few examples on this site. It's not an insurmountable problem, you just have to add the bindings yourself. – Bryan Oakley Jul 02 '15 at 00:06
  • Bryan: I think using a single `update_idletasks()` call would be a better approach than binding the `""` event to a callback—because doing so results in unnecessary and repeated calls `canvas.configure()`. To demonstrate that, in the **Object-oriented solution** just replace the everything in the `__init__()` method from the `self.frame.bind("", self.onFrameConfigure)` line on with `self.populate()`, `self.update_idletasks()`, then finally a single call to `self.onFrameConfigure(None)`. – martineau Apr 08 '18 at 16:19
  • @martineau: you need the binding on `` to handle the case of the window being resized after the GUI starts up. This may never happen, but the unnecessary calls aren't worth worrying over since they typically take just a millisecond or two to run. – Bryan Oakley Apr 08 '18 at 16:30
  • Bryan: Resizing the window afterwards works fine for me, so I think you may be mistaken. I believe this is because the tkinter layout manager takes care things automatically. – martineau Apr 08 '18 at 16:32
  • 2
    @martineau: yes, in this specific case it works fine since the contents of the inner frame never changes. As a general solution, however, using the binding will cover the case when more widgets are added in the frame later, or if the widgets in the inner frame change size. Though, to be honest this example needs an _additional_binding on the `` event of the canvas itself, to handle the case when it is resized. – Bryan Oakley Apr 08 '18 at 16:46
  • Bryan: OK...think I understand, thanks. To recapitulate: The need to do any event binding depends on whether the frame's/canvas' size and therefore it's bounding-box ever changes for some reason—in other words, whether that's dynamic. – martineau Apr 08 '18 at 18:27
  • 4
    @stovfl: wow! That's a mistake. I can't believe nobody has caught that in over 10 years. I've fixed it. – Bryan Oakley Jun 18 '20 at 12:19
  • @Bryan Oakley I am basically using your Procedural solution above and it is working well except for one tiny annoyance - I populate the frame with several rows of text widgets and buttons. The text for the widgets comes from lists and dictionaries built before the gui is created. The gui builds, the buttons show but the text boxes do not show until I move a scroll bar at which point they all appear and fill in correctly and everything works... How can i get the text boxes to show without a scroll bar event? – Pmason Nov 20 '22 at 21:13
17

Make it scrollable

Use this handy class to make the frame containing your widgets scrollable. Follow these steps:

  1. create the frame
  2. display it (pack, grid, etc)
  3. make it scrollable
  4. add widgets inside it
  5. call the update() method

import tkinter as tk
from tkinter import ttk

class Scrollable(tk.Frame):
    """
       Make a frame scrollable with scrollbar on the right.
       After adding or removing widgets to the scrollable frame,
       call the update() method to refresh the scrollable area.
    """

    def __init__(self, frame, width=16):

        scrollbar = tk.Scrollbar(frame, width=width)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=False)

        self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        scrollbar.config(command=self.canvas.yview)

        self.canvas.bind('<Configure>', self.__fill_canvas)

        # base class initialization
        tk.Frame.__init__(self, frame)

        # assign this obj (the inner frame) to the windows item of the canvas
        self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW)


    def __fill_canvas(self, event):
        "Enlarge the windows item to the canvas width"

        canvas_width = event.width
        self.canvas.itemconfig(self.windows_item, width = canvas_width)

    def update(self):
        "Update the canvas and the scrollregion"

        self.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))

Usage example

root = tk.Tk()

header = ttk.Frame(root)
body = ttk.Frame(root)
footer = ttk.Frame(root)
header.pack()
body.pack()
footer.pack()

ttk.Label(header, text="The header").pack()
ttk.Label(footer, text="The Footer").pack()


scrollable_body = Scrollable(body, width=32)

for i in range(30):
    ttk.Button(scrollable_body, text="I'm a button in the scrollable frame").grid()

scrollable_body.update()

root.mainloop()
Tarqez
  • 301
  • 2
  • 7
  • 1
    I get an error in the bind that it's not binding to anything, so I tried it with "". Then it seems to work but ends up the wrong size... how do I make the scrollable frame fill the entire space of its parent and resize dynamically? – samkass Feb 10 '18 at 21:20
  • 1
    Thanks @samkass, adding "" is correct, it was a typo. – Tarqez Feb 11 '18 at 10:29
  • Add options: body.pack(fill=tk.BOTH, expand=True) – Tarqez Mar 26 '18 at 18:03
  • Thank you for a complete working example. Works great. Even better with Tarqez's addition in the comment above. – Rich Lysakowski PhD Oct 20 '21 at 05:51
6

Extends class tk.Frame to support a scrollable Frame
This class is independent from the widgets to be scrolled and can be used to replace a standard tk.Frame.

enter image description here


import tkinter as tk

class ScrollbarFrame(tk.Frame):
    """
    Extends class tk.Frame to support a scrollable Frame 
    This class is independent from the widgets to be scrolled and 
    can be used to replace a standard tk.Frame
    """
    def __init__(self, parent, **kwargs):
        tk.Frame.__init__(self, parent, **kwargs)

        # The Scrollbar, layout to the right
        vsb = tk.Scrollbar(self, orient="vertical")
        vsb.pack(side="right", fill="y")

        # The Canvas which supports the Scrollbar Interface, layout to the left
        self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
        self.canvas.pack(side="left", fill="both", expand=True)

        # Bind the Scrollbar to the self.canvas Scrollbar Interface
        self.canvas.configure(yscrollcommand=vsb.set)
        vsb.configure(command=self.canvas.yview)

        # The Frame to be scrolled, layout into the canvas
        # All widgets to be scrolled have to use this Frame as parent
        self.scrolled_frame = tk.Frame(self.canvas, background=self.canvas.cget('bg'))
        self.canvas.create_window((4, 4), window=self.scrolled_frame, anchor="nw")

        # Configures the scrollregion of the Canvas dynamically
        self.scrolled_frame.bind("<Configure>", self.on_configure)

    def on_configure(self, event):
        """Set the scroll region to encompass the scrolled frame"""
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

Usage:

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        sbf = ScrollbarFrame(self)
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        sbf.grid(row=0, column=0, sticky='nsew')
        # sbf.pack(side="top", fill="both", expand=True)

        # Some data, layout into the sbf.scrolled_frame
        frame = sbf.scrolled_frame
        for row in range(50):
            text = "%s" % row
            tk.Label(frame, text=text,
                     width=3, borderwidth="1", relief="solid") \
                .grid(row=row, column=0)

            text = "this is the second column for row %s" % row
            tk.Label(frame, text=text,
                     background=sbf.scrolled_frame.cget('bg')) \
                .grid(row=row, column=1)


if __name__ == "__main__":
    App().mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • Plus can easily be adapted to provide a pair of scrollbars, vertical and horizontal, by just adding the analogous code. – Wolf Feb 15 '23 at 11:55