Tkinter动画不起作用

时间:2015-02-15 21:31:25

标签: python animation tkinter gif

我正在尝试从我的gif图像中显示动画。从我之前的question开始,我发现Tkinter并没有自动为图像制作动画。我的Tk界面显示图像的第一帧,当我点击按钮播放动画时,它什么也没做。它可能与与按钮相关的命令有关。这是代码:

from Tkinter import *
import Tkinter

root = Tk()

photo_path = "/users/zinedine/downloads/091.gif"
photo = PhotoImage(
    file = photo_path
    )

def run():
    frame = 1
    while True:
        try:
            photo = PhotoImage(
                file = photo_path,
                format = "gif - {}".format(frame)
                )
            frame = frame + 1

        except Exception: # This because I don't know what exception it would raise
            frame = 1
            break

picture = Label(image = photo)
picture.pack()
picture.configure(run())

animate = Button(
    root,
    text = "animate",
    command = run()
    )
animate.pack()

root.geometry("250x250+100+100")
root.mainloop()

2 个答案:

答案 0 :(得分:5)

您可以使用通用Tk窗口小部件after()方法来安排函数在以毫秒为单位指定的延迟后运行。这只发生一次,因此通常函数本身也会调用after()来使过程永久化。

在下面的代码中,定义了一个自定义AnimatedGif容器类,它在列表中分别加载和保存动画序列的所有帧,允许使用[]索引语法对它们进行快速(随机)访问。它使用photo Tk manual page中提到的-index indexvalue 图像格式子选项从文件中读取单个帧。

我从test image网站获得了下面显示的Animation Library

test image

以下是最初启动时的外观。

screenshot of program window at start up

您应该能够使用相同的技术为多个图像或附加到其他类型的窗口小部件的图像设置动画,例如ButtonCanvas个实例。

try:
    from tkinter import *
except ImportError:
    from Tkinter import *  # Python 2


class AnimatedGif(object):
    """ Animated GIF Image Container. """
    def __init__(self, image_file_path):
        # Read in all the frames of a multi-frame gif image.
        self._frames = []

        frame_num = 0  # Number of next frame to read.
        while True:
            try:
                frame = PhotoImage(file=image_file_path,
                                   format="gif -index {}".format(frame_num))
            except TclError:
                break
            self._frames.append(frame)
            frame_num += 1

    def __len__(self):
        return len(self._frames)

    def __getitem__(self, frame_num):
        return self._frames[frame_num]


def update_label_image(label, ani_img, ms_delay, frame_num):
    global cancel_id
    label.configure(image=ani_img[frame_num])
    frame_num = (frame_num+1) % len(ani_img)
    cancel_id = root.after(
        ms_delay, update_label_image, label, ani_img, ms_delay, frame_num)

def enable_animation():
    global cancel_id
    if cancel_id is None:  # Animation not started?
        ms_delay = 1000 // len(ani_img)  # Show all frames in 1000 ms.
        cancel_id = root.after(
            ms_delay, update_label_image, animation, ani_img, ms_delay, 0)

def cancel_animation():
    global cancel_id
    if cancel_id is not None:  # Animation started?
        root.after_cancel(cancel_id)
        cancel_id = None


root = Tk()
root.title("Animation Demo")
root.geometry("250x125+100+100")
ani_img = AnimatedGif("small_globe.gif")
cancel_id = None

animation = Label(image=ani_img[0])  # Display first frame initially.
animation.pack()
Button(root, text="start animation", command=enable_animation).pack()
Button(root, text="stop animation", command=cancel_animation).pack()
Button(root, text="exit", command=root.quit).pack()

root.mainloop()

答案 1 :(得分:3)

这是我的previous answer的替代版本。虽然也基于通用Tk小部件after()方法,但它使用PIL(或它的枕头)模块来读取gif图像文件。使用PIL,不仅可以轻松地从文件中提取每个帧,还可以直接从gif文件中获取动画帧之间的延迟(或#34;持续时间") - 这消除了猜测不同文件应该是什么。

try:
    from tkinter import *
except ImportError:
    from Tkinter import *
from PIL import Image, ImageSequence, ImageTk


class AnimatedGif(object):
    """ Animated GIF Image Container. """
    def __init__(self, image_file_path):
        # Read in all the frames of a multi-frame gif image.
        self._frames = []
        img = Image.open(image_file_path)
        for frame in ImageSequence.Iterator(img):
            photo = ImageTk.PhotoImage(frame)
            photo.delay = frame.info['duration'] * 10  # Add attribute.
            self._frames.append(photo)

    def __len__(self):
        return len(self._frames)

    def __getitem__(self, frame_num):
        return self._frames[frame_num]


def update_label_image(label, ani_img, frame_num):
    """ Change label image to given frame number of AnimatedGif. """
    global cancel_id
    frame = ani_img[frame_num]
    label.configure(image=frame)
    frame_num = (frame_num+1) % len(ani_img)  # Next frame number.
    cancel_id = root.after(frame.delay, update_label_image, label, ani_img, frame_num)

def enable_animation():
    """ Start animation of label image. """
    global cancel_id
    if cancel_id is None:  # Animation not started?
        cancel_id = root.after(ani_img[0].delay, update_label_image, animation, ani_img, 0)

def cancel_animation():
    """ Stop animation of label image. """
    global cancel_id
    if cancel_id is not None:  # Animation started?
        root.after_cancel(cancel_id)
        cancel_id = None


root = Tk()
root.title("Animation Demo")
root.geometry("250x125+100+100")
ani_img = AnimatedGif("small_globe.gif")
cancel_id = None

animation = Label(image=ani_img[0])  # Display first frame initially.
animation.pack()
Button(root, text="start animation", command=enable_animation).pack()
Button(root, text="stop animation", command=cancel_animation).pack()
Button(root, text="exit", command=root.quit).pack()

root.mainloop()