Tkinter/scrollbar/canvas question

agb andrei.bogos at a.gmail.address.com
Tue Jun 21 21:10:34 EDT 2011


Dear Pythonistas,

I've been trying to write a script that results in a set of widgets in a
scrollable widget. Since tkinter is "the" gui for Python (and about the
only one that I can be sure will not require additional software
installation), I went that route, and wrote the following:

from tkinter import *
class ShowList(Frame):
        def __init__(self, root):
                Frame.__init__(self, root)
                self.grid()
                self.draw_widgets()
        def draw_widgets(self):
                cframe = Frame()
                cframe.grid(row=1, sticky=N+S+E+W)
                canv = Canvas(cframe)
                canv["scrollregion"]=canv.bbox(ALL)
                canv.grid(row=0, column=0, sticky=N+S+E+W)
                vscroll = Scrollbar(cframe, orient=VERTICAL, command=canv.yview)
                hscroll = Scrollbar(cframe, orient=HORIZONTAL, command=canv.xview)
                vscroll.grid(row=0, column=1, sticky=N+S)
                hscroll.grid(row=1, column=0, sticky=E+W)
                canv["xscrollcommand"] = hscroll.set
                canv["yscrollcommand"] = vscroll.set
                aframe = Frame(canv)
                canv.create_window(0,0,window=aframe, anchor=N+W)
                for i in range(0,100):
                        Label(aframe, text=str(i), anchor=N+W).grid(row=i, column=0)

root  = Tk()
m=ShowList(root)
root.mainloop()


...which is great in that it does display the list of items (in this case, a
bunch of Labels) but not so great in the fact that the vertical scrollbar's
scroll thumb is the list of the entire scroll trough, making skip down/up a
screenful impossible. Both the horizontal scrollbar and the vertical
scrollbar will scroll until the list is off the screen (not the desired
behavior). 

Initially, I thought the canv["scrollregion"] = canv.bbox(ALL) would take
care of the off-screen scrolling by making the scrollable region no larger
than it needs to be. Reading many examples of Python+tkinter code, intended
to demonstrate the way to code scrollbars and canvases, didn't enlighten me
enough to figure out the bug(s) in my code. Any suggestions as to what I
did wrong?

Many thanks in advance,

--agb

--
http://gall.mine.nu
free to get comics,free chat-1,dc++ (dcplusplus),
mute webcache,group update program,torrent,atomic time server,tool to find your ip



More information about the Python-list mailing list