使用 tkinter 和 类 in Python 的 Gif 动画 3

Gif animation with tkinter and classes in Python 3

我在使我的 gif 在此代码中工作时遇到了一些问题。 它是一种带有图形均衡器的媒体播放器:) 我正在尝试在“pantalla”标签内播放 gif。我的框架变量有一个属性错误。我一直在环顾四周,但无法解决问题。 对不起我的错误我是新手。 任何帮助将不胜感激。

这里是代码:


import os
from tkinter import *
from vlc import Instance
import time


app = Tk()
app.title("JlMultimedia")
app.iconbitmap("C:/Users/Thunder/Documents/Projects/Python/auricular.ico")
app.geometry("340x250")
app.config(bg="blue")

frames = [PhotoImage(file='C:/Users/Thunder/Documents/Projects/Python/imagenes/equalizer1.gif', format = 'gif -index %i' %(i)) for i in range(5)]

def update(self, ind):
    self.frame = frames[ind]
    ind += 1
    print(ind)
    if ind>4: #With this condition it will play gif infinitely
        ind = 0
    app.after(100, update, ind)

class Jlmultimedia:

    def __init__(self):
        self.Player = Instance('--loop')
        

        capa = Frame(app)
        capa.config(bg="#ffffff")
        capa.pack(expand="1")


        capa1 = Frame(capa, bg="#0000ff")
        capa1.config()
        capa1.pack(expand="1")

       
        pantalla = Label(capa1, image=self.frame)
        pantalla.grid(row="0", columnspan="3")
        

        self.playimage = PhotoImage(file="C:/Users/Thunder/Documents/Projects/Python/imagenes/play4.png")
        self.stopimage = PhotoImage(file="C:/Users/Thunder/Documents/Projects/Python/imagenes/stop4.png")
        self.pauseimage = PhotoImage(file="C:/Users/Thunder/Documents/Projects/Python/imagenes/pause4.png")
        self.nextimage = PhotoImage(file="C:/Users/Thunder/Documents/Projects/Python/imagenes/forw4.png")
        self.backimage = PhotoImage(file="C:/Users/Thunder/Documents/Projects/Python/imagenes/rew4.png")

        capa2 = LabelFrame(capa)
        capa2.config(bg="#ffffff")
        capa2.pack(expand="1")

        playboton = Button(capa2, image=self.playimage, bd="0", command=self.play)
        playboton.grid(row="1", column="1")

        self.volVar = IntVar()
        volboton = Scale(capa2, from_=100, to=0, resolution=1, length=90, 
        label="Vol", variable=self.volVar, command=self.set_volume)
        volboton.grid(row="1", column="6")
                     
        stopboton = Button(capa2, image=self.stopimage, bd="0", command=self.stop)
        stopboton.grid(row="1", column="2")

        pauseboton = Button(capa2, image=self.pauseimage, bd="0", command=self.pause)
        pauseboton.grid(row="1", column="4")

        nextboton = Button(capa2, image=self.nextimage, bd="0", command=self.next)
        nextboton.grid(row="1", column="3")

        preboton = Button(capa2, image=self.backimage, bd="0", command=self.previous)
        preboton.grid(row="1", column="5")

        # panel to hold player time slider
        self.timeVar = DoubleVar()
        self.timeSliderLast = 0
        self.timeSlider = Scale(capa1, variable=self.timeVar, command=self.OnTime, 
                                       from_=0, to=1000, orient=HORIZONTAL, length=328, showvalue=0)  # label='Time',
        #self.timeSlider.pack(side=BOTTOM, fill=X, expand=1)
        self.timeSliderUpdate = time.time()
        self.timeSlider.grid(row="1", columnspan="5")

    def addPlaylist(self):
        self.mediaList = self.Player.media_list_new()
        path = r"C:/Users/Thunder/Documents/Projects/Python/musica"
        songs = os.listdir(path)
        for s in songs:
            self.mediaList.add_media(self.Player.media_new(os.path.join(path,s)))
        self.listPlayer = self.Player.media_list_player_new()
        self.listPlayer.set_media_list(self.mediaList)

    def set_volume(self, *unuse):
        vol = min(self.volVar.get(), 100)
        self.listPlayer.get_media_player().audio_set_volume(vol)

    def play(self):
        self.listPlayer.play()
    def next(self):
        self.listPlayer.next()
    def pause(self):
        self.listPlayer.pause()
    def previous(self):
        self.listPlayer.previous()
    def stop(self):
        self.listPlayer.stop()

    def OnTime(self, *unused):
        if self.listPlayer:
            t = self.timeVar.get()
            if self.timeSliderLast != int(t):
                self.listPlayer.get_media_player().set_time(int(t * 1e3))  # milliseconds
                self.timeSliderUpdate = time.time()

myPlayer = Jlmultimedia()
myPlayer.addPlaylist()


app = mainloop()

有几个问题:

  • update()从未执行过
  • 标签 pantalla
  • image 选项也应该在 update()
  • 中更新
  • pantallaJlmultimedia.__init__()里面的局部变量,应该把它做成实例变量self.pantalla
  • self.frame 未定义
  • app = mainloop() 应该是 app.mainloop()

下面是修改后的update():

def update(ind=0):
    myPlayer.pantalla.config(image=frames[ind%len(frames)])
    app.after(100, update, ind+1)

并将pantalla更改为__init__()中的实例变量:

class Jlmultimedia:
    def __init__(self):
        ...
        self.pantalla = Label(capa1)
        self.pantalla(row=0, columnspan=3)
        ...

然后在app.mainloop()之前调用update():

...
update()
app.mainloop()