Is there any way to scroll one widget with another in tkinter?
Here's the code:
from tkinter import *
root = Tk()
def yview(*args):
text_widget_1.yview(*args)
text_widget_2.yview(*args)
scrollbar = Scrollbar(root , orient = VERTICAL , command = yview)
scrollbar.grid(row = 0 , column = 2 , sticky = N+S+E+W)
text_widget_1 = Text(root , width = 3 , height = 25 , yscrollcommand = scrollbar.set , font = "consolas 14")
text_widget_1.grid(row = 0 , column = 0)
text_widget_2 = Text(root , width = 35 , height = 25 , yscrollcommand = scrollbar.set , font = "consolas 14")
text_widget_2.grid(row = 0 , column = 1 , padx = 2)
for i in range(1,500):
text_widget_1.insert(END , f"{i}\n")
text_widget_2.insert(END , f"Line no: {i}\n")
mainloop()
Here, when I move the scrollbar, both the text widgets are scrolled, and everything works fine.
However when I scroll text_widget_1
, text_widget_2
does not scroll.
Similarly when I scroll text_widget_2
, text_widget_1
does not scroll.
What I want to do is that when I scroll one text widget, the other text widget should also scroll at the same time.
Is there any way to achieve this in tkinter?
It would be great if anyone could help me out.
EDIT: I tried to refer to this question(Python tkinter scrolling two TEXT widgets at the same time with arrow keys), but unfortunately, I couldn't understand what's going in that code, so it didn't help much.