简体   繁体   English

如何使用 Tkinter 将多个文本连接到一个滚动条

[英]How to connect multiple text to one scrollbar with Tkinter

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).但我对 2 个小部件文本和滚动条有问题(将一个滚动条连接到两个文本)。

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.我想显示 2 个文本,一个用于编写用户输入的文本,一个用于显示行号。 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.然后我创建一个滚动条,它将在 Y 轴上滚动 2 个文本,所以我想要做的(主要)是将 2 个小部件(文本)连接到一个滚动条。

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.第一次尝试后,我认为我可以将 2 个文本打包到 1 个容器中。 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.我试图创建一个包含 2 个文本的框架(打包到根目录中),我这样做是因为我必须仅将滚动条连接到框架。 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.但它没有用,而且它不允许我在滚动条选项中编写以下代码段: command=frame.yview ,似乎我无法将框架连接到滚动条。

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 Tkinter 将行号添加到文本小部件

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()

There's nothing special about a scrollbar - it just calls a function when you move it.滚动条没有什么特别之处——当你移动它时它只会调用 function。 The API for this function is well defined.此 function 的 API 定义明确。 While it normally should call the yview (or xview ) method of a scrollable window, there's no requirement that it must.虽然它通常应该调用可滚动 window 的yview (或xview )方法,但并不要求它必须这样做。

If you want to control two widgets with a single scrollbar, create a function for your scrollbar that scrolls both windows.如果你想用一个滚动条控制两个小部件,为你的滚动条创建一个 function 来滚动 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.当您在输入新行或使用 cursor 键移动时滚动文本小部件时,您将遇到类似的问题。 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)您需要配置文本小部件的yscrollcommand属性以调用 function 以便它更新滚动条并滚动其他 window (可能还添加其他行号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM