在Python3中使用tkinter和类的GIF动画

人气:213 发布:2022-10-16 标签: vlc gif python-3.x tkinter

问题描述

我在使gif在此代码中工作时遇到一些问题。 这是一种带有图形均衡器的媒体播放器:) 我正在努力让gif在&pantalla&pantalla;品牌内部发挥作用。我的Frame变量有一个属性错误。我一直在四处寻找,但还是解决不了这个问题。 对不起,我犯了错误,我是新手。 任何帮助都将不胜感激。

代码如下:


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选项也应在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()

253