Pysimplegui在图形中使用GIF

2024-10-03 09:08:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的pysimplegui图形中使用gif,但是gif不会移动,有没有办法解决这个问题? 我看到有一种方法可以更新动画,但我无法抓取图形图像来更新它,所以我能做什么?另外,我的屏幕读数没有阻塞任何内容

def criapersonagem(x,y):
    global local_personagem
    global personagem
    personagem = graph.draw_image(filename='personagemgif.gif',
                                  data=None,
                                  location=(x, y))
criarpersonagem(90,60)

这就是我在图表中创建图像的方式


Tags: 方法图形内容屏幕localdef动画gif
1条回答
网友
1楼 · 发布于 2024-10-03 09:08:38

GIF图像中有很多帧,对于动画,必须将每一帧显示为时间流逝

这里有一个例子来说明我是如何做到这一点的。键盘控件<Left><Right><Up><Down>也可以工作。 记住在代码中使用GIF文件

import ctypes
import platform
import threading
from io import BytesIO
from time import sleep

from PIL import Image
import PySimpleGUI as sg

def image_to_data(im):
    with BytesIO() as output:
        im.save(output, format="PNG")
        data = output.getvalue()
    return data

def process_thread():
    global index
    while True:
        sleep(0.01)
        index = (index + 1) % frames
        window.write_event_value('Animation', index)

if platform.system() == "Windows":
    if platform.release() == "7":
        ctypes.windll.user32.SetProcessDPIAware()
    elif platform.release() == "8" or platform.release() == "10":
        ctypes.windll.shcore.SetProcessDpiAwareness(1)

im = Image.open("D:/1.gif")
width, height = im.size
frames = im.n_frames

graph_width, graph_height = size = (640, 640)

layout = [[sg.Graph(size, (0, 0), size, background_color='green', key='GRAPH')]]
window = sg.Window("Animation", layout, return_keyboard_events=True, finalize=True)

graph = window['GRAPH']
index = 0
im.seek(index)
data = image_to_data(im)
x, y = location = (graph_width//2-width//2, height)
item = graph.draw_image(data=data, location=location)

step = 20

thread = threading.Thread(target=process_thread, daemon=True)
thread.start()

while True:

    event, values = window.read(timeout=50)

    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Animation':
        im.seek(index)
        data = image_to_data(im)
        item_new = graph.draw_image(data=data, location=location)
        graph.delete_figure(item)
        item = item_new
    elif event == "Left:37":
        x = x - step if x >= step else x
        location = (x, y)
    elif event == "Right:39":
        x = x + step if x <= graph_width - width - step else x
        location = (x, y)
    elif event == "Up:38":
        y = y + step if y <= graph_height - step else y
        location = (x, y)
    elif event == "Down:40":
        y = y - step if y >= height + step else y
        location = (x, y)

window.close()

相关问题 更多 >