繁体   English   中英

Pysimplegui 在图中使用 gif

[英]Pysimplegui use gifs in graph

我试图在我的 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)

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

GIF 图像中有很多帧,对于 animation,您必须随着时间的推移显示每一帧。

这是展示我如何做到这一点的示例。 键盘控制<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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM