简体   繁体   中英

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.

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)? I don't want any kind of lag when I past or scroll the text. Am I forced to use 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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