Tkinter/scrollbar/canvas question

A

agb

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
 
C

Chris Angelico

This is the third time I've tried to post this reply.  If you see multiple answers from me, that's why.

All three came through on the mailing list, but out of order - this
one came in second.

Chris Angelico
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top