Tkinter cpu activity after pasting text from file

If I take a 25MB / 190,000 line text file and pull it into a text widget, the process completes quickly, but I can still see python.exe using 50% cpu for another minute or so. The larger the file, the longer it takes for the processor to drop to 0% utilization. When I load multiple files into different text widgets, they are instantly loaded into the widget, but the CPU stays at 50% and the GUI is very slow until it finishes everything it does in the backend. Can anyone explain to me why the processor is still in use and why its impact on performance if the text is already in the widgets? What do we have to do? How to get around this?

from tkinter import *


file = r"C:\path\to\large\file.txt"

def doit():
    with open(file, 'r') as f:
        txt.insert('end', ''.join(f))
        f.close()
main = Tk()

txt = Text(main)
txt.grid(row=0)

btn = Button(main, text="click here", command=doit)
btn.grid(row=1, columnspan=2)

main.mainloop()

      

I thought it might be because I was processing the file line by line, rather than loading the entire file into RAM. I tried readlines () but get the same results.

+3


source to share


1 answer


Most likely, it calculates where the line break goes for the lines passing through the area currently visible on the screen. With the numbers you gave, the lines are on average over 130 characters; if some of them significantly exceed this, this is the situation that Tkinter is known to be slower.

Perhaps you can turn off word wrap (by adjusting the text with wrap=NONE

), which will probably require adding a horizontal scroll bar. If that is not acceptable, it is possible that adding new rows might help if there is some natural point to insert them into your data.



Note that ''.join(f)

- a rather inefficient way of reading the entire file on one line - just use it f.read()

for that.

+4


source







All Articles