包装许多小部件时的Tkinter性能

时间:2016-07-11 17:14:31

标签: python python-2.7 user-interface tkinter

我正在使用Tkinter在python中创建GUI,并且在将许多小部件打包到屏幕上时遇到了一些性能问题,例如,打包50x50网格按钮需要几秒钟。

这似乎是将小部件绘制(或安排?)到屏幕上的过程,花费时间。我尝试过使用网格和放置几何管理器。

我想知道使用多处理是否可以加快速度?我欢迎任何其他建议,以便可以大大加快这一点。

import Tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
for i in range(50):
    for j in range(50):
        widget = tk.Frame(frame, bd=2, relief='raised', height=10, width=10)
        widget.grid(row=i, column=j) # using place is barely quicker
tk.Button(root, text='pack', command=frame.pack).pack()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

正如评论中所建议的那样,最好的解决方案最终是使用画布并在其上绘制,而不是打包这么多小部件,这似乎对其速度有绝对的限制。

我使用内置按钮有效地截取屏幕截图来创建未被点击和点击状态的图像。假设图像文件存储为got message '001 : 1234567885' got message '001 : 1234567885 -> 101' {phoneNo=1234567885, handsetId=101} im_up.png,则下面的代码说明了画布解决方案。

im_down.png
相关问题