1

Im coding my personal text editor. But i have a problem with the 2 widget text and the scrollbar (connect one scrollbar to two text).

What is my idea and logic (at the beginning)?

I want to display 2 text, one for writing text entered by user, and one to display the number of the line. I pack both of them, in the root. Then i create a scrollbar, that will scroll on Y axes the 2 text, so what i want to do (mainly) is to connect 2 widget (text) to one scrollbar.

But it didn't work.

This system absolutely doesn't work, are there any suggest or fix to fix this first idea?

Other ideas that i found.

After the first attempt, i thought that i can pack the 2 texts into 1 container. I tried to create a frame (packed into root) that contains the 2 texts, i did this because i have to connect the scrollbar only to the frame. But it didn't work, moreover it didnt allow me to write the following snippet: command=frame.yview in the scrollbar option, it seems that i cant connect frame to scrollbar.

So:

I will ask u if my reasoning are good, and how to solve. If not what can i do?

Similar question found on Google: (but that i dont undestand)

How to scroll two parallel text widgets with one scrollbar?

Tkinter adding line number to text widget

from tkinter import *


root = Tk()

root.geometry("480x540+100+100")
root.config(cursor='')

line = Text(root, bg="light grey", font="Roman 24", width=4)
line.pack(side=LEFT, fill=BOTH)

text = Text(root, bg="grey", font="Roman 24")
text.pack(side=LEFT, fill=BOTH, expand=True)

scrollbar = Scrollbar(text, orient=VERTICAL, command=(line.yview, text.yview))
text.configure(yscrollcommand=scrollbar.set)
line.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side=RIGHT, fill=Y)

for n in range(50):
    line.insert("{}.0".format(n+1), "{}\n".format(n+1))
    text.insert("{}.0".format(n+1), "Line no. {}\n".format(n+1))

if __name__ == '__main__':
    root.mainloop()
Riccardo
  • 192
  • 2
  • 2
  • 11

1 Answers1

4

There's nothing special about a scrollbar - it just calls a function when you move it. The API for this function is well defined. While it normally should call the yview (or xview) method of a scrollable window, there's no requirement that it must.

If you want to control two widgets with a single scrollbar, create a function for your scrollbar that scrolls both windows.

def multiple_yview(*args):
    line.yview(*args)
    text.yview(*args)
scrollbar = Scrollbar(text, orient=VERTICAL, command=multiple_yview)

You will have a similar problem when you scroll the text widget while entering new lines or moving around with cursor keys. You'll need to configure the yscrollcommand attribute of the text widget to call a function so that it both updates the scrollbar and also scrolls the other window (and maybe also add additional line numbers)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Ok, but with your code i have just solved the problem with scrollbar, but theres another problem. If i scroll up/down texts with scrollbar all ok, but if i use mouse wheel it doesnt work. What should i do? – Riccardo Oct 01 '19 at 19:21
  • This is a different problem, ask a new question for it Riccardo. – Reblochon Masque Oct 01 '19 at 23:53