简体   繁体   English

Tkinter 文本滚动滞后问题

[英]Tkinter Text scroll lag issue

I have this super simple implementation of a textBox:我有一个文本框的这个超级简单的实现:

root = Tk()
root.geometry('1650x900')
center_windows(root)
root.resizable(0, 0)
scrollbar = Scrollbar(root)
old_xml_text = Text(root, wrap=WORD, yscrollcommand=scrollbar.set, height=40, width=60)    
old_xml_text.grid(row=0, column=0,pady=(100,0),padx=(50,50),sticky='we')    
scrollbar.config(command=old_xml_text.yview)

The problem is that if I paste there a long text (2k lines) it became extremely laggy during scrolling.问题是,如果我在那里粘贴一个长文本(2k 行),它在滚动过程中变得非常滞后。

How can I solve this?我该如何解决这个问题?

If there is no solution using Tkinter , is there any other way to achieve this using other package (in Python)?如果没有使用Tkinter的解决方案,是否有任何其他方法可以使用其他 package (在 Python 中)实现此目的? I don't want any kind of lag when I past or scroll the text.当我过去或滚动文本时,我不希望有任何延迟。 Am I forced to use C/C++?我是否被迫使用 C/C++?

Any suggestion appreciated.任何建议表示赞赏。

The problem lies in the computation time it takes the Text widget to insert all line breaks and thereby to compute the total number of lines and the current position.问题在于文本小部件插入所有换行符从而计算总行数和当前 position 所需的计算时间。 While scrolling, this computation is too slow and you get lags and the scrollbar is rendered useless.滚动时,这种计算太慢了,你会出现滞后,滚动条变得无用。

If you want to speed it up and remove the lags, you have to insert line breaks manually, so that no line extends the 60 character width of your text window.如果您想加快速度并消除滞后,您必须手动插入换行符,以便没有行扩展文本 window 的 60 个字符宽度。 If you insert line breaks and don't pass it to the wrapper, it won't lag.如果您插入换行符并且不将其传递给包装器,则它不会滞后。 See the following example for reference:请参阅以下示例以供参考:

from tkinter import *

root = Tk()
root.geometry('800x600')
#center_windows(root)
root.resizable(0, 0)

scrollbar = Scrollbar(root)
old_xml_text = Text(root, wrap=WORD, yscrollcommand=scrollbar.set, width=60,height=10)    
old_xml_text.grid(row=0, column=0,pady=(100,0),padx=(0,0),sticky=N+S+E)    
scrollbar.config(command=old_xml_text.yview)
scrollbar.grid(row=0,column=1,pady=(100,0),sticky=N+S+W)

scrollbar2 = Scrollbar(root)
old_xml_text2 = Text(root, wrap=WORD, yscrollcommand=scrollbar2.set, width=60,height=10)    
old_xml_text2.grid(row=2, column=0,pady=(100,0),padx=(0,0),sticky=N+S+E)    
scrollbar2.config(command=old_xml_text2.yview)
scrollbar2.grid(row=2,column=1,pady=(100,0),sticky=N+S+W)


old_xml_text.insert(END,"Hello World, this is a test of reaction times\n"*1000)
old_xml_text2.insert(END,"Hello World, this is a test of reaction times"*1000)

You can see that the Text widget with line breaks is perfectly well scrollable while the one that uses the Text widget wrapper is not.您可以看到带有换行符的 Text 小部件可以很好地滚动,而使用 Text 小部件包装器的小部件则不能。

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

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