改变Python,Pygame,Mutagen,Tkinter队列中的mp3歌曲

时间:2017-01-26 20:43:00

标签: python-3.x tkinter pygame mutagen

我是python的新手,我正在尝试构建一个可以将所有mp3歌曲添加到该文件夹​​列表中的音乐播放器。

我可以看到列表中的所有歌曲,但是当我点击下一个按钮时,它只会播放下一首歌曲和之前的一首歌曲。如果我再次点击它再次播放同一首歌。无论如何我可以修复我的代码

我认为方法中存在一些问题,nextsong,prevsong。请帮助我尝试了很多,但无法找到错误的位置

import os


import pygame
from tkinter.filedialog import Tk, Button, askdirectory, Label, Listbox, LEFT, RIGHT

from mutagen.id3 import ID3


root = Tk()


listofsongs=[]
formattedlist = []
realnames = []

index =0


def directorychoose():
    filename = askdirectory()
    os.chdir(filename)

    for file in os.listdir(filename):
        if file.endswith(".mp3"):
            realdir = os.path.realpath(file)
            audio = ID3(realdir)
            realnames.append(audio['TIT2'].text[0])
            listofsongs.append(file)



    for file in realnames:
        formattedlist.append(file+"\n")

    pygame.mixer.init()
    pygame.mixer.music.load(listofsongs[0])
    pygame.mixer.music.play()


def nextsong(event):
    pygame.mixer.music.load(listofsongs[index+1])
    pygame.mixer.music.play()

def prevsong(event):
    pygame.mixer.music.load(listofsongs[index-1])
    pygame.mixer.music.play()

def stopsong(event):
    pygame.mixer.music.stop()

directorychoose()

label = Label(root,text='Music player')
label.pack()

listbox = Listbox(root)

listbox.pack()
for item in formattedlist:
    listbox.insert(0,item)

button = Button(root,text='Next')
button.pack(side=LEFT)
button2 = Button(root,text='Prev')
button2.pack(side=RIGHT)
stopbutton = Button(root,text='Stop')
stopbutton.pack()

button.bind("<Button-1>",nextsong)
button2.bind("<Button-1>",prevsong)
stopbutton.bind("<Button-1>",stopsong)


root.mainloop()

1 个答案:

答案 0 :(得分:1)

问题在于您从未将更改分配给索引,因此它永远不会更改。此外,必须将其作为全局变量放置,因为函数中的变量是在其中创建和销毁的。

你这样做:

x+1

你应该把它改成:

x= x+1

完整代码:

import os


import pygame
from tkinter.filedialog import Tk, Button, askdirectory, Label, Listbox, LEFT, RIGHT

from mutagen.id3 import ID3

root = Tk()

listofsongs = []
formattedlist = []
realnames = []

index = 0

def directorychoose():
    filename = askdirectory()
    os.chdir(filename)

    for file in os.listdir(filename):
        if file.endswith(".mp3"):
            realdir = os.path.realpath(file)
            audio = ID3(realdir)
            realnames.append(audio['TIT2'].text[0])
            listofsongs.append(file)

    for file in realnames:
        formattedlist.append(file + "\n")

    pygame.mixer.init()
    pygame.mixer.music.load(listofsongs[0])
    pygame.mixer.music.play()


def nextsong(event):
    global index
    index += 1
    pygame.mixer.music.load(listofsongs[index])
    pygame.mixer.music.play()


def prevsong(event):
    global index
    index -= 1
    pygame.mixer.music.load(listofsongs[index])
    pygame.mixer.music.play()


def stopsong(event):
    pygame.mixer.music.stop()


directorychoose()

label = Label(root, text='Music player')
label.pack()

listbox = Listbox(root)

listbox.pack()
for item in formattedlist:
    listbox.insert(0, item)

button = Button(root, text='Next')
button.pack(side=LEFT)
button2 = Button(root, text='Prev')
button2.pack(side=RIGHT)
stopbutton = Button(root, text='Stop')
stopbutton.pack()

button.bind("<Button-1>", nextsong)
button2.bind("<Button-1>", prevsong)
stopbutton.bind("<Button-1>", stopsong)

root.mainloop()