2

I need to scroll a "line number" text widget and a "code" text widget simultaneously in an IDE I'm developing. How would I achieve this?

2
  • What have you tried? Have you tried having the scrollbar call a function that calls the yview method of your two widgets? Commented Mar 12, 2016 at 13:08
  • @BryanOakley - I have set the scrollbar the scroll the "code" widgets using self.vsb=tk.Scrollbar(self,command=self.text.yview) . But I do not see how I can set the same for self.numb (the line number widget) without removing the scroll on self.text . Commented Mar 12, 2016 at 14:53

2 Answers 2

2

[EDITED] It's really easy to do this in Tcl, so I figured it had to be possible to get a Tkinter equivalent of the following Tcl procedure:

proc rollon {boxes args} { foreach box $boxes { eval {$box yview} $args } } 

After a few failed efforts, I came up with this, which works:

#!/usr/bin/env python3 from tkinter import * from tkinter import ttk root = Tk() def viewall(*args): global tx, tx2 eval('tx.yview(*args)') eval('tx2.yview(*args)') tx = Text(root, background='white', width = '20', height = '8') tx2 = Text(root, background='white', width = '20', height = '8') rolly = ttk.Scrollbar(root, orient=VERTICAL, command=viewall) tx['yscrollcommand'] = rolly.set tx2['yscrollcommand'] = rolly.set tx.grid(row=0, column=0, sticky=(N, W, E, S)) tx2.grid(row=0, column=1, sticky=(N, W, E, S)) rolly.grid(row=0, column=2, sticky=(N, W, E, S)) root.mainloop() 

Somebody who knows more Python than I do could probably figure out how to do this without listing the "yview" for each text widget separately, but this should get you going.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to use eval. Just call tx.yview(*args).
True. And since the occasions to use a huge number of multi-scrolling widgets will probably be very few, it may not be worth the effort of trying to find out how not to list each "yview" separately. For only two widgets, it wouldn't be worth more than the few seconds it takes to read this comment. :o)
1

My own solution, while not the best, was this: (Forgive the lack of context)

def scroller(self,*args):#Move me self.text.yview(*args) self.numb.yview(*args) def on_textscroll(self, *args):#Move me self.vsb.set(*args) self.scroller('moveto', args[0]) 

1 Comment

A full example can be found here: stackoverflow.com/questions/32038701/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.