Mailing List Archive

Tk Scrollbar
Hi!
I have only an easy question. I think it is possible to bind a scrollbar
and an other widget e.g. a frame with buttons. But I have not found any
literature or programming examples. I want to bind the scrollbar to an
frame and I don't know how. Please help me.
Thanks for links and Python-code or other help.

Sven
Tk Scrollbar [ In reply to ]
Sven Drescher wrote:
>
> Hi!
> I have only an easy question. I think it is possible to bind a scrollbar
> and an other widget e.g. a frame with buttons. But I have not found any
> literature or programming examples. I want to bind the scrollbar to an
> frame and I don't know how. Please help me.
> Thanks for links and Python-code or other help.
>
> Sven

The communication between a widget and a scrollbar is carried out by two
procedures:

1. The scrollbar calls the widget's "yview" (or "xview") method to
cause it to scroll to a new position.

2. The widget calls the scrollbar's "set" method to indicate
to invoke a change in the sliders position or size.

The only widgets which implement the "yview" method are "Canvas",
"Listbox", and "Text". (And to be very clear here, this implementation
and the connection between the two widgets is at the tcl/tk level.)

So...
To add scrolling capabilities to an arbitrary widget, you would need to
implement the "yview" command (in its several versions) on the widget.
I don't know if this would need to be implemented in tcl, or if you
could rig things so that the scrollbar's call to "yview" would come all
the way back into python so that you could implement it in python.

--
Dr. Gary Herron <gherron@aw.sgi.com>
206-287-5616
Alias | Wavefront
1218 3rd Ave, Suite 800, Seattle WA 98101
Tk Scrollbar [ In reply to ]
Sven Drescher <Sven.Drescher@dlr.de> writes:

> Hi!
> I have only an easy question. I think it is possible to bind a scrollbar
> and an other widget e.g. a frame with buttons. But I have not found any
> literature or programming examples. I want to bind the scrollbar to an
> frame and I don't know how. Please help me.
> Thanks for links and Python-code or other help.
>
> Sven

Find the pmw (python mega-widget) module, it has a Pmw.ScrolledFrame
widget which I think will do what you want. I haven't used it myself
but the documentations says is works.

Pmw homepage is:

http://www.dscpl.com.au/pmw/

--
Tim Evans
Tk Scrollbar [ In reply to ]
> I have only an easy question. I think it is possible to bind a scrollbar
> and an other widget e.g. a frame with buttons. But I have not found any
> literature or programming examples. I want to bind the scrollbar to an
> frame and I don't know how. Please help me.

you cannot. period.

but you can put the frame in a canvas, and
add scrollbars to the canvas. the following
example does this (and more).

</F>

#
# tk045 -- scrolled frame with autohiding scrollbars
#
# fredrik lundh, august 1998
#
# fredrik@pythonware.com
# http://www.pythonware.com
#

from Tkinter import *

class AutoScrollbar(Scrollbar):
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise TclError, "cannot use pack with this widget"
def place(self, **kw):
raise TclError, "cannot use place with this widget"

#
# create scrolled canvas

root = Tk()

vscrollbar = AutoScrollbar(root)
vscrollbar.grid(row=0, column=1, sticky=N+S)
hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)

canvas = Canvas(root,
yscrollcommand=vscrollbar.set,
xscrollcommand=hscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)

vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)

# make the canvas expandable
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

#
# create canvas contents

frame = Frame(canvas)
frame.rowconfigure(1, weight=1)
frame.columnconfigure(1, weight=1)

rows = 5
for i in range(1,rows):
for j in range(1,10):
button = Button(frame, padx=7, pady=7, text="[%d,%d]" % (i,j))
button.grid(row=i, column=j, sticky='news')

canvas.create_window(0, 0, anchor=NW, window=frame)

frame.update_idletasks()

canvas.config(scrollregion=canvas.bbox("all"))

root.mainloop()