I would like to make a Tkinter
class
, based on the answer here, which is a Frame
that automatically shows/hides Scrollbar
s around the content as necessary.
The answer that I linked to above works perfectly for my needs, with the caveat that because it's not contained within a class
, it's not reusable. I figured this would be pretty quick and easy, but for some reason, my AutoScrollbar
s never appear once I refactor the code into its own class
, regardless of how much or little of the contents of the Frame
is hidden by the window resizing.
# This class is unchanged from the other answer that I linked to,
# but I'll reproduce its source code below for convenience.
from autoScrollbar import AutoScrollbar
from Tkinter import Button, Canvas, Frame, HORIZONTAL, Tk, VERTICAL
# This is the class I made - something isn't right with it.
class AutoScrollable(Frame):
def __init__(self, top, *args, **kwargs):
Frame.__init__(self, top, *args, **kwargs)
hscrollbar = AutoScrollbar(self, orient = HORIZONTAL)
hscrollbar.grid(row = 1, column = 0, sticky = 'ew')
vscrollbar = AutoScrollbar(self, orient = VERTICAL)
vscrollbar.grid(row = 0, column = 1, sticky = 'ns')
canvas = Canvas(self, xscrollcommand = hscrollbar.set,
yscrollcommand = vscrollbar.set)
canvas.grid(row = 0, column = 0, sticky = 'nsew')
hscrollbar.config(command = canvas.xview)
vscrollbar.config(command = canvas.yview)
# Make the canvas expandable
self.grid_rowconfigure(0, weight = 1)
self.grid_columnconfigure(0, weight = 1)
# Create the canvas contents
self.frame = Frame(canvas)
self.frame.rowconfigure(1, weight = 1)
self.frame.columnconfigure(1, weight = 1)
canvas.create_window(0, 0, window = self.frame, anchor = 'nw')
canvas.config(scrollregion = canvas.bbox('all'))
# This is an example of using my new class I defined above.
# It's how I know my class isn't working quite right.
root = Tk()
autoScrollable = AutoScrollable(root)
autoScrollable.grid(row = 0, column = 0, sticky = 'news')
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
for i in xrange(10):
for j in xrange(10):
button = Button(autoScrollable.frame, text = '%d, %d' % (i, j))
button.grid(row = i, column = j, sticky = 'news')
autoScrollable.frame.update_idletasks()
root.mainloop()
Here's the source for autoScrollbar
, which I'm including because I import it in the above source, but I don't think the actual problem is here.
# Adapted from here: http://effbot.org/zone/tkinter-autoscrollbar.htm
from Tkinter import Scrollbar
class AutoScrollbar(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:
self.grid_remove()
else:
self.grid()
Scrollbar.set(self, lo, hi)
def pack(self, *args, **kwargs):
raise TclError('Cannot use pack with this widget.')
def place(self, *args, **kwargs):
raise TclError('Cannot use pack with this widget.')
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
toself.canvas
in your AutoScrollable class and callright after
You could also bind a
<Configure>
event to yourautoScrollable.frame
which calls both theupdate_idletasks()
and updates thescrollregion
. That way, you don't have to worry about calling it yourself anymore because they update whenever the Frame's size is changed.