我一直在尝试使用python创建音乐播放器,但是我有一个错误,由于某种原因,任何碰巧具有专辑封面/图片的歌曲都无法正常工作,并且应用程序崩溃了。我一直在尝试使用代码而非手动方式删除专辑封面,但到目前为止失败了:
import os
from tkinter import *
import tkinter as tk
from tkinter import filedialog
from pygame import mixer
import tkinter.messagebox
root = Tk()
# create the menu bar
menubar = Menu(root)
root.config(menu=menubar)
# create sub menus
subMenu = Menu(menubar)
def choosefile():
global filename
filename = filedialog.askopenfilename()
menubar.add_cascade(label="file", menu=subMenu)
subMenu.add_command(label="Open", command=choosefile)
subMenu.add_command(label="Exit", command=root.destroy)
def about_us():
tkinter.messagebox.showinfo('About MyMusicPlayer',
'this is the basis of our emotion music player we are starting off with a regular '
'music player and then we will incorrporate the emotion recognition part')
subMenu = Menu(menubar)
menubar.add_cascade(label="Help", menu=subMenu)
subMenu.add_command(label="About Us", command=about_us)
mixer.init() # initializing the mixer
# root.geometry('300x300')
img = tk.Image("photo", file="MyMusicIcon.gif")
root.tk.call('wm', 'iconphoto', root.w, img)
root.title("MyMusicPlayer")
text = Label(root, text='lets make some noise!')
text.pack(pady=10)
def play_music():
try:
paused # checks whether the paused variable has been initialized
except NameError:
try:
mixer.music.load(filename)
mixer.music.play()
statusbar['text'] = "Playing music" + ' - ' + os.path.basename(filename)
except:
tkinter.messagebox.showerror('File not found',
'MyMyusicPlayer could not find the file, please try again')
else:
mixer.music.unpause()
statusbar['text'] = "Music has resumed"
def stop_music():
mixer.music.stop()
statusbar['text'] = "Music has stopped"
def pause_music():
global paused
paused = TRUE
mixer.music.pause()
statusbar['text'] = "Music has paused"
def set_vol(val):
volume = int(val) / 100
# set_volume of mixer takes value only from 0 to 1
mixer.music.set_volume(volume)
middleframe = Frame(root)
middleframe.pack(padx=10, pady=10)
PlayPhoto = PhotoImage(file='PlayIcon.png')
Playbtn = Button(middleframe, image=PlayPhoto, command=play_music)
Playbtn.grid(row=0, column=0)
StopPhoto = PhotoImage(file='StopIcon.png')
Stopbtn = Button(middleframe, image=StopPhoto, command=stop_music)
Stopbtn.grid(row=0, column=1)
PausePhoto = PhotoImage(file='images/pause.png')
Pausebtn = Button(middleframe, image=PausePhoto, command=pause_music)
Pausebtn.grid(row=0, column=2)
VolumeScale = Scale(root, from_=0, to=100, orient=HORIZONTAL, command=set_vol)
VolumeScale.set(70)
mixer.music.set_volume(0.7)
VolumeScale.pack(pady=10)
statusbar = Label(root, text="Welcome to MyMusicPlayer", relief=SUNKEN, anchor=W)
statusbar.pack(side=BOTTOM, fill=X)
root.mainloop()