更改队列中的 mp3 歌曲 Python、Pygame、Mutagen、Tkinter

Changing mp3 songs in a queue Python,Pygame,Mutagen,Tkinter

我是 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()

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

您正在这样做:

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()