【问题标题】:Make a fast pixel-changing animation widget in Python 3,在 Python 3 中制作一个快速像素变化的动画小部件,
【发布时间】:2022-08-20 10:49:54
【问题描述】:

我想要的是:

我想要一个具有快速显示*生成的动画小部件(延迟,允许小于 20 秒)并具有运行像素变化动画的特殊功能无延迟(延迟,允许小于 20 毫秒)在 Python 3 中。

错误的方法:

最简单的方法是像素面板。但它的生成速度非常缓慢**。 这是它的代码:

import tkinter as t

tk = t.Tk()
n = 1100
k = 900 # Display sizes
canvas = t.Canvas(tk, width =k, height = n)
canvas.grid(column = 0, row = 0)
ids = [] # Pixel identifiers
for i in range(n):
    id2 = []
    for j in range(k):
        id2.append(cv.create_rectangle(i,j,i+1,j+1,outline = \"black\"))
    ids.append(id2)
def animation(animation_list): # Animation function,animation_list - list of coords and colors tuples(as example,[(1, 0, \"red\"), (2, 1, \"green\")].
    global canvas
    for x, y, color in animation_list:
        canvas.itemconfig(ids[x][y],outline = color)

* 尺寸为 1100x900(990k 像素)
** Debug 显示 22 秒,但花了超过一分钟,而且窗口滞后。

    标签: python python-3.x tkinter animation graphics


    【解决方案1】:

    我找到了!感谢Mike-SMT 的想法!
    这是我的代码:

    import os
    import pygame as pg
    from random import *
    
    main_dir = os.path.split(os.path.abspath(__file__))[0]
    data_dir = os.path.join(main_dir, "data")
    
    
    def show(image):
        screen = pg.display.get_surface()
        screen.fill((255, 255, 255))
                    
        screen.blit(image, (0, 0))
        pg.display.flip()
    
    
    def main():
        pg.init()
    
        pg.display.set_mode((255, 255))
        surface = pg.Surface((255, 255))
    
        pg.display.flip()
    
        t = 0
        s = 1
        while 1:
            if t >= 255:
                s = -4
            if t <= 0:
                s = 4
            t += s
            ar = pg.PixelArray(surface)
            for y in range(255):
                for z in range(255):
                    r, g, b = t%256,y,z
                    ar[z,y] = (r, g, b)
            del ar
            show(surface)        
    
    if __name__ == "__main__":
        main()
    

    但是我没有修复关闭窗口的错误,它也很滞后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2011-10-25
      • 2020-10-29
      • 1970-01-01
      相关资源
      最近更新 更多