0

How do I scroll two commands text1 and text2? Help me please.

from tkinter import *

root = Tk()
root.title('Text editor')
root.geometry(1000x500)

text_scroll = Scrollbar(frame)
text_scroll.pack(side=RIGHT, fill=Y)

text = Text(root, width=500, height=250, font=font, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, yscrollcommand=text_scroll.set, wrap="word")
text.pack(expand="yes", fill=BOTH)

text2 = Text(root, width=500, height=250, font=font, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, wrap="word")
text2.pack(expand="yes", fill=BOTH)

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
dhanielB
  • 3
  • 2
  • 3
    `.geometry()` takes a string argument not integer – Matiiss Apr 14 '21 at 16:55
  • 1
    what exactly is Your issue because running the current code shows one text entry and a scrollbar that partially works as it should – Matiiss Apr 14 '21 at 16:58
  • @Matiiss I think OP is trying to scroll both `tkinter.Text`s at the same time. – TheLizzard Apr 14 '21 at 17:04
  • @TheLizzard oh, could be, but the issue is that I am not seeing the other Text widget at all, why would that be? – Matiiss Apr 14 '21 at 17:07
  • @Matiiss, I had the same problem, width and height for text are in rows and characters, not pixels, divide those numbers by 10 at least. – joao Apr 14 '21 at 17:21

2 Answers2

0

Are you trying to connect 2 tkinter.Texts to scroll them at the same time? If so look at this:

import tkinter as tk


def text_yview(*args):
    text.yview(*args)
    text2.yview(*args)

def scroll_set(*args):
    text_scroll.set(*args)
    text.yview("moveto", args[0])
    text2.yview("moveto", args[0])


root = tk.Tk()

text_scroll = tk.Scrollbar(root)
text_scroll.pack(side="right", fill="y")

text = tk.Text(root, yscrollcommand=scroll_set)
text.pack(side="right")

text2 = tk.Text(root, yscrollcommand=scroll_set)
text2.pack(side="right")

text_scroll.config(command=text_yview)

text.insert("end", "\n".join(str(i) for i in range(30)))
text2.insert("end", "\n".join(str(i) for i in range(30)))

root.mainloop()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
0

If You want to add individual scrollbars on the right side on top of each other to be used for each text widget You can do this:

from tkinter import *

root = Tk()
root.title('Text editor')
root.geometry('1000x500')

scroller_frame = Frame(root)
scroller_frame.pack(side=RIGHT, fill=Y)

text_scroll1 = Scrollbar(scroller_frame)
text_scroll1.pack(side=TOP, fill=Y, expand=True)
text_scroll2 = Scrollbar(scroller_frame)
text_scroll2.pack(side=TOP, fill=Y, expand=True)

text = Text(root, width=50, height=10, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, yscrollcommand=text_scroll1.set, wrap="word")
text.pack(expand="yes", fill=BOTH)

text2 = Text(root, width=50, height=10, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, wrap="word", yscrollcommand=text_scroll2.set)
text2.pack(expand="yes", fill=BOTH)

root.mainloop()
Matiiss
  • 5,970
  • 2
  • 12
  • 29