0

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

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

我认为方法有问题,下一首,上一首。请帮助我尝试了很多但找不到错误在哪里

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

1 回答 1

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()
于 2017-01-26T21:17:21.707 回答