如何利用Python编写本地音乐播放器
发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章主要为大家展示了"如何利用Python编写本地音乐播放器",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何利用Python编写本地音乐播放器"这篇
千家信息网最后更新 2025年11月13日如何利用Python编写本地音乐播放器
这篇文章主要为大家展示了"如何利用Python编写本地音乐播放器",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何利用Python编写本地音乐播放器"这篇文章吧。
先上完整代码:
修改文件夹路径即可运行
# -*- encoding: utf-8 -*-'''@Description: :@Date :2022/03/24 17:43:26@Author :骤&雨@version :1.0'''#导入相关库文件import osimport tkinterimport tkinter.filedialogimport randomimport timeimport threadingimport pygamefrom asyncio.base_tasks import _task_print_stackfrom cProfile import labelfrom email import headerfrom functools import total_orderingfrom importlib import find_loaderfrom logging import rootfrom mimetypes import initfrom re import Xfrom turtle import widthfrom matplotlib.pyplot import pausefrom scipy import rand#设置文件夹路径folder = r'C:\Users\Administrator\Desktop\Python\Python Code\TiquMusicFromVedio'"""@description : 音乐播放,默认播放文件夹内的所有mp3文件---------@param :-------@Returns :-------"""def play(): global folder music =[folder+'\\'+music for music in os.listdir(folder)\ if music.endswith(('.mp3','.wav','.ogg'))] total = len(music) #初始化混音器设备 pygame.mixer.init() while playing: if not pygame.mixer.music.get_busy(): #随机播放一首歌曲 nextMusic = random.choice(music) pygame.mixer.music.load(nextMusic.encode()) #播放一次 pygame.mixer.music.play(1) musicName.set('playing.......'+nextMusic) else: time.sleep(0.3)root = tkinter.Tk()root.title('音乐播放器')root.geometry('700x80+400+300')root.resizable(False,False)#关闭程序时执行的代码def closeWindow(): global playing playing = False try: pygame.mixer.music.stop() pygame.mixer.quit() except: pass root.destroy()root.protocol('WM_DELETE_WINDOW',closeWindow)pause_resume=tkinter.StringVar(root,value = 'NotSet')playing = False#播放按钮def buttonPlayClick(): global folder if not folder: folder = tkinter.filedialog.askdirectory() if not folder: return global playing playing = True #创建一个线程来播放音乐 t = threading.Thread(target=play) t.start() #根据情况禁用或启用相应按钮 buttonPlay['state'] = 'disabled' buttonStop['state'] = 'normal' buttonPause['state'] = 'normal' buttonNext['state'] = 'normal' pause_resume.set('Pause')buttonPlay = tkinter.Button(root,text = 'Play',command=buttonPlayClick)buttonPlay.place(x=20,y=10,width=50,height=20)#终止按钮def buttonStopClick(): global playing playing = False pygame.mixer.music.stop() musicName.set('暂时没有播放音乐') buttonPlay['state'] = 'normal' buttonStop['state'] = 'disabled' buttonPause['state'] = 'disabled'buttonStop = tkinter.Button(root,text='Stop',command=buttonStopClick)buttonStop.place(x=80,y=10,width=50,height=20)buttonStop['state']='disabled'#暂停与恢复 复用按钮def buttonPauseClick(): global playing if pause_resume.get()=='Pause': #playing = False pygame.mixer.music.pause() pause_resume.set('Resume') elif pause_resume.get()=='Resume': #playing = True pygame.mixer.music.unpause() pause_resume.set('Pause')buttonPause = tkinter.Button(root,textvariable=pause_resume,command=buttonPauseClick)buttonPause.place(x=140,y=10,width=50,height=20)buttonPause['state']='disabled'#下一首def buttonNextClick(): global playing playing = False pygame.mixer.music.stop() pygame.mixer.quit() buttonPlayClick()buttonNext = tkinter.Button(root,text='Next',command=buttonNextClick)buttonNext.place(x=200,y=10,width=50,height=20)buttonNext['state']='disabled'musicName = tkinter.StringVar(root,value='暂时没有播放音乐!')labelName = tkinter.Label(root,textvariable=musicName)labelName.place(x=0,y=40,width=700,height=20)#启动消息循环root.mainloop()运行结果如下:

以上是"如何利用Python编写本地音乐播放器"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
音乐
播放器
按钮
文件
内容
文件夹
篇文章
代码
路径
一首
学习
帮助
运行
库文件
情况
易懂
更多
条理
歌曲
消息
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全类型的电影
螺旋模型适合什么软件开发
数据库三张表建立关系
浙江互助软件开发
火箭软件开发
数据库没有服务器名称
网络安全专家李满意
数据库约束电话号码为11位
逆战能刷的免费服务器
骎骎网络技术有限公司地址
数据库%意思
宁夏自然资源厅网络安全培训班
国内著名论文数据库
济南沃德网络技术有限公司
软件开发要背编程代码吗
数据库连接密码怎么查
ubuntu 服务器安全
民航网络安全自查报告
微信小程序需要服务器
天峻网络安全宣传
主角重生互联网科技的小说
贷记卡逾期报送金融数据库
金融类软件开发面试题目
软件开发一个月赚几百万
数据库原理及应用教程习题
网络安全法鼓励数据依法利用
手机控制lunix服务器软件
安防网络技术人员
网络安全等级定级企业
ibm的专业软件开发