Tkinter Scrolling

Eric Brunel eric_brunel at despammed.com
Fri Feb 2 10:26:02 EST 2007


On Thu, 01 Feb 2007 20:26:08 +0100, D <duncanm255 at hotmail.com> wrote:
> Bob Greschke wrote:
>> The typical way to do it is to make a scrolling canvas and
>> pack the buttons and other stuff into an empty Frame() and then pack
>> the frame on to the canvas, which I haven't had to do yet.
>>
>> Bob
> Thanks, Bob - have you seen any examples of the latter approach (using
> a canvas and frame)?  Sounds rather confusing, but it definitely seems
> like the approach I need to take.  Thanks again.

Here you are:

-----------------------------------------------------------
 from Tkinter import *

## Main window
root = Tk()
## Grid sizing behavior in window
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
## Canvas
cnv = Canvas(root)
cnv.grid(row=0, column=0, sticky='nswe')
## Scrollbars for canvas
hScroll = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
hScroll.grid(row=1, column=0, sticky='we')
vScroll = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
vScroll.grid(row=0, column=1, sticky='ns')
cnv.configure(xscrollcommand=hScroll.set, yscrollcommand=vScroll.set)
## Frame in canvas
frm = Frame(cnv)
## This puts the frame in the canvas's scrollable zone
cnv.create_window(0, 0, window=frm, anchor='nw')
## Frame contents
for i in range(20):
   b = Button(frm, text='Button n#%s' % i, width=40)
   b.pack(side=TOP, padx=2, pady=2)
## Update display to get correct dimensions
frm.update_idletasks()
## Configure size of canvas's scrollable zone
cnv.configure(scrollregion=(0, 0, frm.winfo_width(), frm.winfo_height()))
## Go!
root.mainloop()
-----------------------------------------------------------

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list