[英]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.我目前正在使用 tkinter 开发一个 GUI,它由一个文本小部件组成,应该充当 XML 代码编辑器。 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.
我使用标记对某些关键字(如 xml 标记)进行颜色标记,但不幸的是,此操作非常慢,您可以看到 gui 线程需要花费大量时间来突出显示所有内容。 Is there a way to speed this up (eg multiple threads that change the gui or something similar)?
有没有办法加快这个速度(例如,改变gui或类似的东西的多个线程)? Or is there a GUI framework that allows to build more responsive guis?
或者是否有允许构建更具响应性的 guis 的 GUI 框架?
A lot of GUI toolkits (including tkinter
) are not thread-safe;许多 GUI 工具包(包括
tkinter
)不是线程安全的; so you should not issue GUI calls from multiple threads.所以你不应该从多个线程发出 GUI 调用。
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. Python 具有
cProfile
模块形式的内置分析器。 For example, to profile my unlock-excel.py
script, I would use the following command:例如,要分析我的
unlock-excel.py
脚本,我将使用以下命令:
python3 -m cProfile -s cumulative unlock-excel.py ~/foo.xlsm | less
It also produces output for tkinter
programs like unlock-excel.pyw
;它还产生用于输出
tkinter
程序等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}
.在后一种情况下,
tkinter
调用将显示为例如__init__.py:1281(mainloop)
或{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
.有了这个,您应该能够确定缓慢是在您的代码中还是在
tkinter
发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.