首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python Turtle制作GIF动画

用Python Turtle制作GIF动画
EN

Stack Overflow用户
提问于 2020-05-15 09:29:38
回答 1查看 1.9K关注 0票数 1

我找到了一种使用Python显示动画GIF的方法,并且我正在尝试使它与一起工作。

我可以用下面的代码显示动画gif,但是有一些问题。

1)由于某种原因,让.grid(row=0, column=0)将图像从屏幕上移开。2)图像有一个我不想显示的周边边界。

我尝试过使用各种参数的.place(),但没有任何参数将图像放在屏幕上。

对于如何将图像放置在没有边界的特定位置,有什么建议吗?

代码语言:javascript
复制
# main file

import turtle
import tkinter_gif

screen = turtle.Screen()
canvas = screen.getcanvas()
gif_window = tkinter_gif.ImageLabel(canvas)
gif_window.grid(row=1, column=0)
gif_window.load("giphy.gif")
turtle.done()
代码语言:javascript
复制
# tkinter_gif.py

import tkinter as tk
from PIL import Image, ImageTk
from itertools import count

class ImageLabel(tk.Label):
    """a label that displays images, and plays them if they are gifs"""
    def load(self, im):
        if isinstance(im, str):
            im = Image.open(im)
        self.loc = 0
        self.frames = []

        try:
            for i in count(1):
                self.frames.append(ImageTk.PhotoImage(im.copy()))
                im.seek(i)
        except EOFError:
            pass

        try:
            self.delay = im.info['duration']
        except:
            self.delay = 100

        if len(self.frames) == 1:
            self.config(image=self.frames[0])
        else:
            self.next_frame()

    def unload(self):
        self.config(image=None)
        self.frames = None

    def next_frame(self):
        if self.frames:
            self.loc += 1
            self.loc %= len(self.frames)
            self.config(image=self.frames[self.loc])
            self.after(self.delay, self.next_frame)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-15 14:51:52

我建议将ImageLabeltk.Label改为Canvas的图像项,如下所示:

代码语言:javascript
复制
class ImageLabel:
    def __init__(self, canvas):
        self.canvas = canvas

    def load(self, im, x=0, y=0):
        # create a canvas image item
        self.image = self.canvas.create_image(x, y, image=None)
        self.canvas.tag_lower(self.image)

        if isinstance(im, str):
            im = Image.open(im)

        self.frames = []
        try:
            for i in count(1):
                self.frames.append(ImageTk.PhotoImage(im.copy()))
                im.seek(i)
        except EOFError:
            pass

        try:
            self.delay = im.info['duration']
        except:
            self.delay = 100

        num_frames = len(self.frames)
        if num_frames == 1:
            self.canvas.itemconfig(self.image, image=self.frames[0])
        else:
            self.next_frame(0, num_frames)

    def unload(self):
        self.canvas.delete(self.image)
        self.frames = None

    def next_frame(self, loc, total):
        if self.frames:
            self.canvas.itemconfig(self.image, image=self.frames[loc])
            loc = (loc + 1) % total
            self.canvas.after(self.delay, self.next_frame, loc, total)

然后将其加载到特定位置:

代码语言:javascript
复制
gif_window = tkinter_gif.ImageLabel(canvas)
gif_window.load("giphy.gif", -200, -200) # 0, 0 is the center of canvas
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61816038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档