Scrolling tkinter Canvas

Daniel Chamberlin daniel769 at earthlink.net
Fri Mar 23 02:08:12 CET 2001


I am unable to scroll a canvas.  could someone look at the code squib below
and point out what needs to be changed?
essentially what happens is that the Canvas fills up and stretches to fill
the screen, the scrollbar remains unchanged, and the remaining labels go
somewhere where they can't be seen.  what i would like to happen is for the
canvas's size to be limited, and for the scrollbar to control what is
viewed.  i have gotten scrollbars to work with text widgets, but i am unable
to get this to do what i want.

thanks for any help,
doug

from Tkinter import *
class contain:
    def __init__(self, parent):
        self.f = Frame(parent, width=30, height=30)
        self.f.pack()
        self.c=Canvas(self.f)
        self.s=Scrollbar(self.f, orient='horizontal')
        self.c.config(xscrollcommand=self.s.set)
        self.s.config(command=self.c.xview)
        for i in range(50):
            Label(self.c, text='label #' + str(i)).pack(side=LEFT)
        self.c.pack(expand=NO)
        self.s.pack(expand=YES, fill=X)

if __name__=='__main__':
    root = Tk()
    x  = contain(root)
    mainloop




More information about the Python-list mailing list