如何在tkinter中只播放一次gif?

huangapple go评论59阅读模式
英文:

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(&#39;mygif.gif&#39;)
frames = [ImageTk.PhotoImage(frame) for frame in ImageSequence.Iterator(im)]

def update(idx=0):
    if idx &lt; 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=&#39;mygif.gif&#39;,format = &#39;gif -index %i&#39; %(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>



huangapple
  • 本文由 发表于 2023年2月24日 09:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定