[Tkinter-discuss] scrolling more widgets with one scrollbar

Michael Lange klappnase at web.de
Thu Feb 3 18:55:40 CET 2011


Hi again,

Thus spoketh Michael Lange <klappnase at web.de> 
unto us on Thu, 3 Feb 2011 11:56:35 +0100:


> I set up a minimal example that shows how to use this technique to set
> up modified key bindings that allow to scroll two text widgets
> synchronously with the Up and Down keys:
> 

At a second glance my example isn't so nice either, because it does not
only sync the yview but also the cursor position, which is most likely
not wanted. However this can be easily fixed by restoring the "slave"
widget's cursor position with something like:

    def down(event):
        oldinsert = slave[event.widget].index('insert')
        root.tk.call('tk::TextSetCursor', slave[event.widget],
                root.tk.call('tk::TextUpDownLine', event.widget, 1))
        slave[event.widget].mark_set('insert', oldinsert)

Doing this for lots of event sequences is quite a pita though, maybe
wrapping it like this is a bit better:

slave = {t1: t2, t2: t1}
def callback(textwidget, *args):
    oldinsert = slave[textwidget].index('insert')
    root.tk.call(*args)
    slave[textwidget].mark_set('insert', oldinsert)

for t in (t1, t2):
    def down(event):
        callback(event.widget, 'tk::TextSetCursor', slave[event.widget],
                root.tk.call('tk::TextUpDownLine', event.widget, 1))
    t.bind('<Down>', down, add=True)

    def up(event):
        callback(event.widget, 'tk::TextSetCursor', slave[event.widget],
                root.tk.call('tk::TextUpDownLine', event.widget, -1))
    t.bind('<Up>', up, add=True)

Unfortunately nothing that uses just the yview() methods seems to work,
too bad that the most "obvious" thing, simply doing

    text2.yview(text1.yview ()) 

is not supported.

Regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

But Captain -- the engines can't take this much longer!


More information about the Tkinter-discuss mailing list