当前位置: 代码迷 >> 综合 >> Python音乐播放器(tkinter)
  详细解决方案

Python音乐播放器(tkinter)

热度:77   发布时间:2023-12-05 23:48:08.0

写的不是很完整,不过简单的效果还是有的。对于音乐的播放,暂停,停止,和上一曲,下一曲功能都可以的,但是暂停是停止的效果。

用tkinter写的界面,还引用了第三方模块pygame

效果截图如下:

注释不是很多,不过应该很容易理解,不是很理解的可以给我评论


musicPlay.py 调用主程序代码
importtkinterfrommusicListimport MusicList
frommusicButtonControlimport MusicButtonControl
frommusicLyricTextimport MusicLyricTextwin= tkinter.Tk()
win.title("音乐播放器")
win.geometry("700x500+200+100")musicList= MusicList(win)
musicButton= MusicButtonControl(win, musicList)
musicLyric= MusicLyricText(win)win.mainloop()musicList.py 书写音乐列表,插入本地音乐歌名importtkinter
importos
importpygameclassMusicList(tkinter.Frame):
def__init__(self,master):
self.frame= tkinter.Frame(master)
self.frame.pack(side=tkinter.LEFT,fill=tkinter.Y)self.lv= tkinter.StringVar()
self.listBox= tkinter.Listbox(self.frame,selectmode=tkinter.BROWSE,width=30,
height=30,bg="#FFFACD",listvariable=self.lv)
self.listBox.pack()self.addMusicName()self.listBox.bind("<Double-Button-1>",self.playMusic)defplayMusic(self,event):
pygame.mixer.init()
pygame.mixer.music.load(self.getCurrentMusicPath())
pygame.mixer.music.play()defgetCurrentMusicPath(self):
path= r"E:\Python-1704\python\day15\homework\播放音乐器\music"
# self.listBox.select_set(0)
foritem in range(self.listBox.size()):
musicAbsPath= path +"\\"+self.listBox.get(item)
ifself.listBox.selection_includes(item):
path= musicAbsPath
# print("-----", path)
returnpath# 添加音乐曲目
defaddMusicName(self):
path= r"E:\Python-1704\python\day15\homework\播放音乐器\music"
musicNameList= os.listdir(path)
formusicNamein musicNameList:
path1= os.path.join(path, musicName)
path1list= os.path.splitext(path1)
ifpath1list[-1]== ".mp3":
self.listBox.insert(tkinter.END, musicName)
musicButtonControl.py 控制音乐按钮(播放,暂停,停止,上一曲,下一曲)importtkinter
importpygame
importosclassMusicButtonControl(tkinter.Frame):
def__init__(self,master,otherMusicList):
self.frame= tkinter.Frame(master)
self.frame.pack(side=tkinter.TOP,fill=tkinter.Y)# 加载音乐列表
self.otherMusicList= otherMusicList
self.loadMusic()
print("======音乐加载完成")
self.buttonPlay= tkinter.Button(self.frame,text="播放",
command=self.playMusic,
width=8,height=2,bg='#FFEC8B')
self.buttonPlay.pack(side=tkinter.LEFT,fill=tkinter.X)self.buttonPause= tkinter.Button(self.frame,text="暂停",
command=self.pauseMusic,
width=8,height=2,bg='#FFEC8B')
self.buttonPause.pack(side=tkinter.LEFT,fill=tkinter.X)self.buttonStop= tkinter.Button(self.frame,text="停止",
command=self.stopMusic,
width=8,height=2,bg='#FFEC8B')
self.buttonStop.pack(side=tkinter.LEFT,fill=tkinter.X)self.buttonPrevious= tkinter.Button(self.frame,text="上一曲",
command=self.previousMusic,
width=8,height=2,bg='#FFEC8B')
self.buttonPrevious.pack(side=tkinter.LEFT,fill=tkinter.X)self.buttonNext= tkinter.Button(self.frame,text="下一曲",
command=self.nextMusic,
width=8,height=2,bg='#FFEC8B')
self.buttonNext.pack(side=tkinter.LEFT,fill=tkinter.X)# self.textLyric = tkinter.Text(self.frame, bg="#FFDEAD", width=8,height=50)
# self.textLyric.pack(side=tkinter.LEFT, fill=tkinter.X)defloadMusic(self):
pygame.mixer.init()
# self.otherMusicList.listBox.select_set(0)
# musicFilePath = r"E:\Python-1704\python\day15\homework\播放音乐器\music"
# print(self.otherMusicList.listBox.size())
# print("播放:", self.otherMusicList.getMusicPath())defplayMusic(self):
pygame.mixer.music.load(self.otherMusicList.getCurrentMusicPath())
pygame.mixer.music.play()
print(self.otherMusicList.getCurrentMusicPath())defpauseMusic(self):
pygame.mixer.music.pause()defstopMusic(self):
pygame.mixer.music.stop()defpreviousMusic(self):
path= r"E:\Python-1704\python\day15\homework\播放音乐器\music"
currentMusicPath= self.otherMusicList.getCurrentMusicPath()formusicpathIndexin range(self.otherMusicList.listBox.size()):
ismusic= 0
musicAbs1Path= path +"\\"+self.otherMusicList.listBox.get(musicpathIndex)
ifcurrentMusicPath== musicAbs1Path:
ismusic= musicpathIndex
ismusic-= 1
musicAbsPath= path +"\\"+self.otherMusicList.listBox.get(ismusic)
ifismusic < 0:
pygame.mixer.music.load(path+ "\\"+self.otherMusicList.listBox.get(0))
pygame.mixer.music.play()
break
pygame.mixer.music.load(musicAbsPath)
# 显示正在播放的歌曲,并取消上一首歌曲的选中
self.otherMusicList.listBox.select_clear(musicpathIndex)
self.otherMusicList.listBox.select_set(ismusic)
pygame.mixer.music.play()
breakdefnextMusic(self):
path= r"E:\Python-1704\python\day15\homework\播放音乐器\music"
currentMusicPath= self.otherMusicList.getCurrentMusicPath()formusicpathIndexin range(self.otherMusicList.listBox.size()):
ismusic= 0
musicAbs1Path= path +"\\"+self.otherMusicList.listBox.get(musicpathIndex)
ifcurrentMusicPath== musicAbs1Path:
ismusic= musicpathIndex
ismusic+= 1
ifismusic >= self.otherMusicList.listBox.size():
pygame.mixer.music.load(path+ "\\"+self.otherMusicList.listBox.get(self.otherMusicList.listBox.size()-1))
pygame.mixer.music.play()
break
musicAbsPath= path +"\\"+self.otherMusicList.listBox.get(ismusic)
pygame.mixer.music.load(musicAbsPath)
# 显示正在播放的歌曲,并取消上一首歌曲的选中
self.otherMusicList.listBox.select_clear(musicpathIndex)
self.otherMusicList.listBox.select_set(ismusic)
pygame.mixer.music.play()
breakmusicLyricText.py 歌词文本框importtkinter
importos
importtimeclassMusicLyricText(tkinter.Frame):
def__init__(self,master):
self.frame= tkinter.Frame(master)
self.frame.pack(side=tkinter.TOP,fill=tkinter.Y)self.textLyric= tkinter.Text(self.frame,bg="#FFDEAD",height=50)
self.textLyric.pack()