scrollbar dependencies

Eric Brunel eric_brunel at despammed.com
Thu Mar 3 04:38:45 EST 2005


On 3 Mar 2005 01:06:48 -0800, Marion <supermarion3 at gmail.com> wrote:
> Hello.
>
> I am using Tkinter and Pmw.
> I would like to build 2 canvases/frames that are scrollable together
> horizontally, and independently vertically (so, the vertical
> scrollbars should not be moved by the horizontal one).
>
> So I built a Pmw.ScrolledFrame() containing the 2 canvases,
> horizontally scrollable.

I'd not go that way: a Pmw.ScrolledFrame is not supposed to be used this way, and you may have great troubles making it work as you want...

[snip]
> Is this possible or not ? If not, does somebody allready did that another way ?

Here is a solution using only pure-Tkinter:

--DualCanvas.py--------------------------------------------------
 from Tkinter import *

## Main window
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)

## First canvas
c1 = Canvas(root, width=200, height=100, bd=2, relief=SUNKEN,
                   scrollregion=(0, 0, 500, 500))
c1.grid(row=0, column=0, sticky='nswe')

## Second canvas
c2 = Canvas(root, width=200, height=100, bd=2, relief=SUNKEN,
                   scrollregion=(0, 0, 500, 500))
c2.grid(row=1, column=0, sticky='nswe')

## Special function scroll both canvases horizontally
def xscrollboth(*args):
   c1.xview(*args)
   c2.xview(*args)

## Horizontal scrollbar for both canvases
hScroll = Scrollbar(root, orient=HORIZONTAL, command=xscrollboth)
hScroll.grid(row=2, column=0, sticky='we')

## Vertical scrollbars
vScroll1 = Scrollbar(orient=VERTICAL, command=c1.yview)
vScroll1.grid(row=0, column=1, sticky='ns')

vScroll2 = Scrollbar(orient=VERTICAL, command=c2.yview)
vScroll2.grid(row=1, column=1, sticky='ns')

## Associate scrollbars to canvases
c1.configure(xscrollcommand=hScroll.set, yscrollcommand=vScroll1.set)
c2.configure(xscrollcommand=hScroll.set, yscrollcommand=vScroll2.set)

## Put a few things in canvases so that we see what's going on
c1.create_oval(80, 80, 120, 120, fill='red')
c1.create_oval(380, 80, 420, 120, fill='red')
c1.create_oval(80, 380, 120, 420, fill='red')
c1.create_oval(380, 380, 420, 420, fill='red')

c2.create_oval(80, 80, 120, 120, fill='blue')
c2.create_oval(380, 80, 420, 120, fill='blue')
c2.create_oval(80, 380, 120, 420, fill='blue')
c2.create_oval(380, 380, 420, 420, fill='blue')

## Go!
root.mainloop()
-----------------------------------------------------------------

The "trick" is simply to create a function accepting the same arguments as the xview methods on canvases and forward these arguments to the xview methods of both canvases. You then pass this function to the command option of both scrollbars, and voilà. The link canvas -> scrollbar is done the usual way.

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



More information about the Python-list mailing list