Tkinter Scrolling

D

D

I'm sure this is a simple question to the Tkinter experts - I have a
very basic Tkinter application that consists of 1 master window and
buttons within that window. My problem is that, I need to be able to
scroll (up and down) when I get to the point that the buttons go off
the screen. What's the easiest way to do this? Thanks in advance.
 
B

Bob Greschke

I'm sure this is a simple question to the Tkinter experts - I have a
very basic Tkinter application that consists of 1 master window and
buttons within that window. My problem is that, I need to be able to
scroll (up and down) when I get to the point that the buttons go off
the screen. What's the easiest way to do this? Thanks in advance.

The super-easiest way is to make the "background" a Text() widget, add
its scrollbars, then add the buttons and whatever else to it. Of
course you are then limited to the 'typewriter layout' of widget
placement. 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
 
D

D

Bob said:
The super-easiest way is to make the "background" a Text() widget, add
its scrollbars, then add the buttons and whatever else to it. Of
course you are then limited to the 'typewriter layout' of widget
placement. 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.
 
E

Eric Brunel

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()
 
D

D

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()

Thanks, Eric - exactly what I needed!
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top