英文:
How to run gif one time in tkinter?
问题
以下是代码的翻译部分:
from tkinter import *
import time
import os
root = Tk()
frameCnt = 12
frames = [PhotoImage(file='mygif.gif', format='gif -index %i' % (i)) for i in range(frameCnt)]
def update(ind):
frame = frames[ind]
ind += 1
if ind == frameCnt:
ind = 0
label.configure(image=frame)
root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()
请注意,代码部分未进行翻译。
英文:
from tkinter import *
import time
import os
root = Tk()
frameCnt = 12
frames = [PhotoImage(file='mygif.gif',format = 'gif -index %i' %(i)) for i in range(frameCnt)]
def update(ind):
frame = frames[ind]
ind += 1
if ind == frameCnt:
ind = 0
label.configure(image=frame)
root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()
I want to run file gif one time when i opened window .
this code run gif infinity.
答案1
得分: 1
如果您只想播放GIF一次,就像Bryan的评论所说,无需重置索引。
硬编码的frameCnt
不够灵活,因为如果使用另一个GIF图像,它需要进行更改。
此外,与Pillow
模块相比,使用tkinter.PhotoImage()
从GIF图像加载帧速度较慢,所以我建议使用Pillow
模块。
import tkinter as tk
from PIL import Image, ImageTk, ImageSequence
root = tk.Tk()
label = tk.Label(root)
label.pack()
im = Image.open('mygif.gif')
frames = [ImageTk.PhotoImage(frame) for frame in ImageSequence.Iterator(im)]
def update(idx=0):
if idx < len(frames):
label.configure(image=frames[idx])
root.after(100, update, idx+1)
update() # 开始动画
root.mainloop()
英文:
If you want to animate the GIF only once, you don't need to reset the index as said by Bryan's comment.
Hard-coded frameCnt
is not flexible because it need to be changed if another GIF image is used.
Also using tkinter.PhotoImage()
to load frames from a GIF image is a bit slow when comparing with Pillow
module, so I would suggest to use Pillow
module.
import tkinter as tk
from PIL import Image, ImageTk, ImageSequence
root = tk.Tk()
label = tk.Label(root)
label.pack()
im = Image.open('mygif.gif')
frames = [ImageTk.PhotoImage(frame) for frame in ImageSequence.Iterator(im)]
def update(idx=0):
if idx < len(frames):
label.configure(image=frames[idx])
root.after(100, update, idx+1)
update() # start the animation
root.mainloop()
答案2
得分: 0
如Bryan Oakley评论所述,我们已经在检查计数,所以我们只是退出函数。
from tkinter import *
import time
import os
root = Tk()
frameCnt = 12
frames = [PhotoImage(file='mygif.gif', format='gif -index %i' % (i)) for i in range(frameCnt)]
def update(ind):
frame = frames[ind]
ind += 1
label.configure(image=frame)
if ind == frameCnt:
return
root.after(500, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()
英文:
As Bryan Oakley commented, we're already checking the count, so we just exit the function.
from tkinter import *
import time
import os
root = Tk()
frameCnt = 12
frames = [PhotoImage(file='mygif.gif',format = 'gif -index %i' %(i)) for i in range(frameCnt)]
def update(ind):
frame = frames[ind]
ind += 1
label.configure(image=frame)
if ind == frameCnt:
return
root.after(500, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论