【问题标题】:Tkinter GIF Animation falters and is pixelatedTkinter GIF 动画步履蹒跚并且像素化
【发布时间】:2021-08-14 16:05:28
【问题描述】:

我在 After Effects 中制作了一个加载轮动画,我正在尝试在 python 中将它与 tkinter 一起使用。尽管动画是每秒 60 帧,但它会动摇并且不会显示整个帧。这是我的代码:

from tkinter import * 
from PIL import Image

root = Tk()
root.geometry("1920x1080")

image1 = Image.open("LoadingWheel.gif")
framesTotal = image1.n_frames

animation = [PhotoImage(file="LoadingWheel.gif", format=f'gif -index {i}') for i in range(framesTotal)]

def update(ind):
    frame = animation[ind]
    label.configure(image=frame)
    
    ind += 1
    if ind == framesTotal:
        ind = 0

    root.after(60, update, ind)

label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()

第一张图是我运行脚本时的截图,第二张图是它的样子!

我希望有人知道如何解决这个问题!

提前致谢!

【问题讨论】:

    标签: python animation tkinter gif


    【解决方案1】:

    使用ImageTk.PhotoImage

    from tkinter import * 
    from PIL import Image, ImageTk
    
    root = Tk()
    #root.geometry("1920x1080")
    
    image1 = Image.open(r"loading.gif")
    framesTotal = image1.n_frames
    
    play_back_delay = 30
    animation = []
    
    def loadGif():
        for x in range(framesTotal):
            frame = ImageTk.PhotoImage(image1.copy())
            animation.append(frame)
            image1.seek(x)
    
    
    def update(ind):
        frame = animation[ind]
        label.configure(image=frame)
        
        ind += 1
        if ind == framesTotal:
            ind = 0
    
        root.after(play_back_delay, update, ind)
    
    label = Label(root)
    label.pack()
    loadGif()
    update(0)
    root.mainloop()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2020-08-30
      • 2015-04-06
      • 2022-06-17
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多