【问题标题】:Animated GIF with Python Turtle带有 Python Turtle 的 GIF 动画
【发布时间】:2020-05-15 09:29:38
【问题描述】:

我找到了一种使用 Python tkinter 显示动画 GIF 的方法,我正在尝试让它与 Python Turtle Graphics 一起使用。

我可以使用以下代码显示动画 gif,但存在问题。

1) 出于某种原因,拥有.grid(row=0, column=0) 会使图像脱离屏幕。 2)图像有一个我不想显示的周围边框。

我用各种参数尝试了.place(),但没有一个将图像放在屏幕上。

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

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

【问题讨论】:

标签: python tkinter turtle-graphics animated-gif


【解决方案1】:

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

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)

然后在特定位置加载:

gif_window = tkinter_gif.ImageLabel(canvas)
gif_window.load("giphy.gif", -200, -200) # 0, 0 is the center of canvas

【讨论】:

  • , -200, -200:你为什么不用canvas.create_image(..., anchor='nw', ...
  • 这只是一个示例,由 OP 调整参数/选项。
  • 这只是一个例子:一如既往,但是为什么
猜你喜欢
  • 2013-12-22
  • 2016-08-23
  • 2020-11-13
  • 2013-12-17
  • 2018-08-29
  • 2013-01-21
  • 2019-08-11
  • 1970-01-01
  • 2021-03-19
相关资源
最近更新 更多