[Tutor] Tkinter Expanding Canvas with AutoScrollBar

Alexander Grant a.n.grant at gmail.com
Mon Feb 9 18:29:34 CET 2009


Hi Tutors,
Thanks in advance for all the advice you've already given to others... it
has been extremely useful!

I'm working on a project, and I wanted to have a header, a footer and an
expanding canvas with  (possibly) many widgets inside it.

I simply modified Fred Lundh's auto-hiding scrollbar code found here -
http://effbot.org/zone/tkinter-autoscrollbar.htm
It is almost working as hoped - but I have the problem that the canvas only
resizes smaller, not larger - even when the canvas is too large to display
in the original window!

I'll attach my code... I'm just not sure what's going wrong:


#################################################
from Tkinter import *

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:
            # grid_remove is currently missing from Tkinter!
            self.tk.call("grid", "remove", self)
        else:
            self.grid()
        Scrollbar.set(self, lo, hi)
    def pack(self, **kw):
        raise TclError, "cannot use pack with this widget"
    def place(self, **kw):
        raise TclError, "cannot use place with this widget"

#
# create scrolled canvas

al = Tk()
al.columnconfigure(0,weight=1)
al.rowconfigure(0,weight=1)

header = Frame(al)
header.pack(side= TOP, expand = YES)

htxt = Label(header,text='TOP TOP TOP... this is the TOP')
htxt.pack()

footer = Frame(al)
footer.pack(side = BOTTOM, expand = YES)

ftxt = Label(footer,text='BOTTOM BOTTOM BOTTOM... this is the BOTTOM!')
ftxt.pack()

root = LabelFrame(al, bd=2, text='Data',relief='groove',padx=5, pady=5)
root.pack(side = TOP, expand = YES)

vscrollbar = AutoScrollbar(root)
vscrollbar.grid(row=0, column=1, sticky=N+S)
hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)

canvas = Canvas(root,
                yscrollcommand=vscrollbar.set,
                xscrollcommand=hscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)

vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)

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

# create canvas contents

frame = Frame(canvas)
frame.rowconfigure(1, weight=1)
frame.columnconfigure(1, weight=1)

rows = 20
for i in range(1,rows):
    for j in range(1,20):
        button = Button(frame, padx=7, pady=7, text="[%d,%d]" % (i,j))
        button.grid(row=i, column=j, sticky='news')

canvas.create_window(0, 0, anchor=NW, window=frame)

frame.update_idletasks()

canvas.config(scrollregion=canvas.bbox("all"))

root.mainloop()
#################################################



Thanks,

Al
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090209/8a9ac84e/attachment-0001.htm>


More information about the Tutor mailing list