问题描述
我试图来显示我的GIF图像动画。从我的previous ,我发现Tkinter的不动画自动图像。我Tk的界面显示图像的第一帧,当我点击按钮来播放其动画,它什么都不做。它很可能是与与该按钮相关联的命令。这里的code:
从Tkinter的进口*
进口的Tkinter根= TK()photo_path =/users/zinedine/downloads/091.gif
照片=光象(
文件= photo_path
)高清的run():
帧= 1
而真正的:
尝试:
照片=光象(
文件= photo_path,
格式=GIF - {}。格式(帧)
)
帧=帧+ 1 除例外情况:#这是因为我不知道这会引起什么异常
帧= 1
打破照片= LABEL(图像=照片)
picture.pack()
picture.configure(的run())动画=按钮(
根,
文字=动画,
命令=的run()
)
animate.pack()root.geometry(250x250的+ 100 + 100)
root.mainloop()
您可以使用通用部件塔卡的方法来安排一个函数以毫秒为单位指定的延迟后运行。这只是发生一次,所以一般函数本身也要求后()
延续的过程。
在自定义下code AnimatedGif
容器类定义加载并保存动画序列的所有帧单独的列表,它可以快速(随机)对它们的访问使用 []
索引语法。它读取使用 -index
的 indexvalue
的图像格式子选项提到从文件中的单个帧照片Tk的手册页。
下面是启动时的东西应该是什么样子。我得到了使用从网站。
您应该能够使用相同的技术来制作动画附加到其他类型的小部件,如按钮
和画布多个图像或那些
实例。
从Tkinter的进口*类AnimatedGif(对象):
动画GIF图像集装箱
高清__init __(自我,image_file_path):
self.image_file_path = image_file_path
self._frames = []
self._load() 高清__len __(个体经营):
返回LEN(self._frames) 高清__getitem __(自我,的frame_num):
返回self._frames [的frame_num] 高清_load(个体经营):
读入多帧GIF图像的所有帧。
而真正的:
下一帧的frame_num = LEN(self._frames)#数字阅读
尝试:
帧=光象(文件= self.image_file_path,
格式=GIF -index {}。格式(的frame_num))
除了TclError:
打破
self._frames.append(帧)高清update_label_image(标签,ani_img,MS_DELAY,的frame_num):
全球cancel_id
label.configure(图像= ani_img [的frame_num])
的frame_num =(的frame_num + 1)%LEN(ani_img)
cancel_id = root.after(
MS_DELAY,update_label_image,标签,ani_img,MS_DELAY,的frame_num)高清enable_animation():
全球cancel_id
MS_DELAY = 1000 // LEN(ani_img)
cancel_id = root.after(
MS_DELAY,update_label_image,标签,ani_img,MS_DELAY,0)高清cancel_animation():
全球cancel_id
如果cancel_id不是无:
root.after_cancel(cancel_id)
cancel_id =无cancel_id =无
根= TK()
root.title(动画演示)image_file_path =small_globe.gif
ani_img = AnimatedGif(image_file_path)标签=标签(图片= ani_img [0])#显示第一帧开始
label.pack()
start_animation =按钮(根,文本=启动动画命令= enable_animation)
start_animation.pack()
stop_animation =按钮(根,文本=停止动画命令= cancel_animation)
stop_animation.pack()
exit_program =按钮(根,文本=退出命令= root.quit)
exit_program.pack()root.geometry(250x125 + 100 + 100)
root.mainloop()
I am trying to display the animation from my gif image. From my previous question, I discovered that Tkinter doesn't animate images automatically. My Tk interface shows the first frame of the image, and when I click the button to play its animation, it does nothing. It's likely something to do with the command associated with the button. Here's the code:
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()
You can use the universal Tk widget after()
method to schedule a function to run after a specified delay given in milliseconds. This only happens once, so typically the function itself also calls after()
to perpetuate the process.
In the code below a custom AnimatedGif
container class is defined which loads and holds all the frames of animated sequence separately in a list which allows quick (random) access to them using []
indexing syntax. It reads individual frames from the file using the -index
indexvalue
image format suboption mentioned on the photo Tk manual page.
Here's how things should look when it starts. I got the test image being used from the Animation Library website.
You should be able use the same technique to animate multiple images or those that are attached to other kinds of widgets, such as Button
and Canvas
instances.
from Tkinter import *
class AnimatedGif(object):
""" Animated GIF Image Container. """
def __init__(self, image_file_path):
self.image_file_path = image_file_path
self._frames = []
self._load()
def __len__(self):
return len(self._frames)
def __getitem__(self, frame_num):
return self._frames[frame_num]
def _load(self):
""" Read in all the frames of a multi-frame gif image. """
while True:
frame_num = len(self._frames) # number of next frame to read
try:
frame = PhotoImage(file=self.image_file_path,
format="gif -index {}".format(frame_num))
except TclError:
break
self._frames.append(frame)
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
ms_delay = 1000 // len(ani_img)
cancel_id = root.after(
ms_delay, update_label_image, label, ani_img, ms_delay, 0)
def cancel_animation():
global cancel_id
if cancel_id is not None:
root.after_cancel(cancel_id)
cancel_id = None
cancel_id = None
root = Tk()
root.title("Animation Demo")
image_file_path = "small_globe.gif"
ani_img = AnimatedGif(image_file_path)
label = Label(image=ani_img[0]) # display first frame initially
label.pack()
start_animation = Button(root, text="start animation", command=enable_animation)
start_animation.pack()
stop_animation = Button(root, text="stop animation", command=cancel_animation)
stop_animation.pack()
exit_program = Button(root, text="exit", command=root.quit)
exit_program.pack()
root.geometry("250x125+100+100")
root.mainloop()
这篇关于Tkinter的动画将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!