Skip to content Skip to sidebar Skip to footer

Python Tkinter Scrollable Frame Class?

I would like to make a Tkinter class, based on the answer here, which is a Frame that automatically shows/hides Scrollbars around the content as necessary. The answer that I linked

Solution 1:

You're calling canvas.config(scrollregion = canvas.bbox('all')) when the canvas is still empty, effectively making the scrollregion (0, 0, 1, 1).

You should wait with defining the scrollregion until you have the widgets in your Frame. To do that you should rename canvas to self.canvas in your AutoScrollable class and call

autoScrollable.canvas.config(scrollregion = autoScrollable.canvas.bbox('all'))

right after

autoScrollable.frame.update_idletasks()

You could also bind a <Configure> event to your autoScrollable.frame which calls both the update_idletasks() and updates the scrollregion. That way, you don't have to worry about calling it yourself anymore because they update whenever the Frame's size is changed.

    self.frame.bind('<Configure>', self.frame_changed)

def frame_changed(self, event):
    self.frame.update_idletasks()
    self.canvas.config(scrollregion = self.canvas.bbox('all'))

Post a Comment for "Python Tkinter Scrollable Frame Class?"