打包许多小部件时的 Tkinter 性能 [英] Tkinter performance when packing many widgets

查看:23
本文介绍了打包许多小部件时的 Tkinter 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a GUI in python using Tkinter and have had some performance issues when packing many widgets onto the screen, for example packing a 50x50 grid of buttons takes a few seconds.

这似乎是在屏幕上绘制(或排列?)小部件的过程需要时间.我试过同时使用网格和放置几何管理器.

It seems to be the process of drawing (or arranging?) the widgets onto the screen which takes the time. I have tried using both the grid and place geometry managers.

我想知道使用多处理是否可以加快速度?我欢迎任何其他可以显着加快速度的建议.

I am wondering if using multiprocessing might speed this up? I would welcome any other suggestions for any way in which this could be significantly sped up.

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()

推荐答案

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

As suggested in the comments, the best solution did end up being to use a canvas and draw onto it, rather than to pack so many widgets, which seems to have absolute limitations on its speed.

我通过有效地截取屏幕截图来使用内置按钮来创建未点击和点击状态的图像.假设图片文件存储为im_up.pngim_down.png,那么下面的代码说明了canvas方案.

I used the built-in buttons by effectively taking a screenshot to create images of the unclicked and clicked states. Suppose the image files are stored as im_up.png and im_down.png, then the code below illustrates the canvas solution.

import Tkinter as tk
# Uses Python Imaging Library.
from PIL import Image as ImagePIL, ImageTk

root = tk.Tk()
canvas = tk.Canvas(root, height=500, width=500, highlightthickness=0)
canvas.pack()
images = dict()
for name in ['up', 'down']:
    im = ImagePIL.open('im_{}.png'.format(name))
    # Resize the image to 10 pixels square.
    im = im.resize((10, 10), ImagePIL.ANTIALIAS)
    images[name] = ImageTk.PhotoImage(im, name=name)
def set_cell_image(coord, image):
    # Get position of centre of cell.
    x, y = ((p + 0.5) * 10 for p in coord)
    canvas.create_image(x, y, image=image)
for i in range(50):
    for j in range(50):
        set_cell_image((i, j), images['up'])
def click(event):
    coord = tuple(getattr(event, p)/10 for p in ['x', 'y'])
    set_cell_image(coord, images['down'])
canvas.bind('<Button-1>', click)
root.mainloop()

这篇关于打包许多小部件时的 Tkinter 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆