Pygame音乐播放器

时间:2017-05-07 16:06:50

标签: python tkinter pygame listboxitem audio-player

我正在尝试使用pygame和tkinter开发一个简单的音乐应用程序。但是,从列表框中点击时,我似乎无法播放特定的歌曲。当我尝试选择一首歌时,它突出显示并给我一个错误而不是播放。我使用的方法是file_selection方法。我的代码和错误显示如下:

这些是我使用的模块:

import os
import pygame
from mutagen.id3 import ID3
from tkinter import *
from tkinter.filedialog import askdirectory
from tkinter import ttk

用于从列表框中选择的方法:

def file_selection(self):
    pygame.mixer.music.load(listbox.curselection)
    pygame.mixer.music.play()

我如何从目录中获取mp3文件并将它们加载到列表框中:

# Method used to extract songs/playlists from folders
def directorychooser(event):  # Opens the File Explorer
    directory = askdirectory()
    os.chdir(directory)  # Opens the Files Explorer

    for files in os.listdir(directory):
        if files.endswith(".mp3"):  # Only picks files with .mp3 extensions
            realdir = os.path.realpath(files)  # get the full path of files
            audio = ID3(realdir)  # Extracts the meta data of the song 
            realnames.append(audio['TIT2'].text[0])  # Loads the metadata 
            songlist.append(files)  # Adds the files to the playlist
            print(files)

    for items in realnames:
        listbox.insert(END, items)  # Places the meta data on the listbox 

songlabel.grid(row=1, column=3)  # Places the current song's title 

# List Box
listbox = Listbox(root, bg='black', fg='white', height=10, 
          selectmode=BROWSE)  # Creates the box that holds the playlist
listbox.bind('<<ListboxSelect>>', file_selection)
listbox.grid(row=2, column=3)

这是我在尝试运行代码时收到的错误:

Exception in Tkinter callback Traceback (most recent call last): File "File 
Location", 
line 1699, in call return self.func(*args) File "File Location", line 94, in
file_selection pygame.mixer.music.load(listbox.curselection) pygame.error: 
Couldn't read from RWops

0 个答案:

没有答案
相关问题