简体   繁体   中英

How to make a tkinter GUI faster?

everybody,

i am currently working on a GUI with tkinter, which consists of a text widget that is supposed to act as an XML code editor. I use tagging to color mark certain keywords like xml tags, but unfortunately this operation is very slow and you can see that the gui thread takes a lot of time to highlight everything. Is there a way to speed this up (eg multiple threads that change the gui or something similar)? Or is there a GUI framework that allows to build more responsive guis?

A lot of GUI toolkits (including tkinter ) are not thread-safe; so you should not issue GUI calls from multiple threads.

If you want to know why a program is slow, in general you have to profile it. A profiler is a tool that helps you see where a program is spending its time. Python has a built-in profiler in the form of the cProfile module. For example, to profile my unlock-excel.py script, I would use the following command:

python3 -m cProfile -s cumulative unlock-excel.py ~/foo.xlsm | less

It also produces output for tkinter programs like unlock-excel.pyw ;

python3 -m cProfile -s cumulative unlock-excel.pyw

In the latter case, tkinter calls will show up as eg __init__.py:1281(mainloop) or {method 'call' of '_tkinter.tkapp' objects} .

With this, you should be able to determine if the slowness is in your code or if it happens in tkinter .

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