Tkinter scrolling

Felipe Ochoa felipeochoa0918 at
Wed Oct 28 00:50:39 CET 2009

So I've been trying to put Frederik Lundh's ScrolledFrame ( ) into a class, and
have the frame do more useful things. I tried switching out the
references to root to a frame, and moving it into a class. It runs,
until you try to make the frame/canvas/root smaller than its contents.
>From an interactive session, saying something like:

>>> f.botframe["width"] = 200
>>> f.topframe["width"] = 200
>>> f.canvas["width"] = 200

will make the window blink three times, and return to its original
size. Here's the code:

import tkinter as tk

class AutoScrollbar(tk.Scrollbar):
    # a scrollbar that hides itself if it's not needed.  only
    # works if you use the grid geometry manager.
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            # grid_remove is currently missing from Tkinter!
  "grid", "remove", self)
        tk.Scrollbar.set(self, lo, hi)
    def pack(self, **kw):
        raise tk.TclError("cannot use pack with this widget")
    def place(self, **kw):
        raise tk.TclError( "cannot use place with this widget")

class ScrollFrame:
    def __init__(self,master):
        self.topframe = tk.Frame(master)
        vscrollbar = AutoScrollbar(self.topframe)
        vscrollbar.grid(row=0, column=1, sticky='ns')
        hscrollbar = AutoScrollbar(self.topframe,
        hscrollbar.grid(row=1, column=0, sticky='ew')

        self.canvas = tk.Canvas(self.topframe,
        self.canvas.grid(row=0, column=0, sticky='news')


        # make the canvas expandable
        self.topframe.grid_rowconfigure(0, weight=1)
        self.topframe.grid_columnconfigure(0, weight=1)

        # create canvas contents

        self.botframe = tk.Frame(self.canvas)
        self.canvas.create_window(0, 0, anchor='nw',



Thanks in advance!!

More information about the Python-list mailing list