python3音乐播放器简单实现代码


Posted in Python onApril 20, 2020

本文实例为大家分享了python3音乐播放器的关键代码,供大家参考,具体内容如下

from tkinter import *
from traceback import *
from win32com.client import Dispatch
import time,eyed3,threading
 
name = []
 
def openfile(index = [1]):
 global total,name
  
 filenames = filedialog.askopenfilenames(title = "音乐播放器",filetypes =[("mp3文件","*.mp3"),("WMA文件","*.wma"),("WAV文件","*.wav")])
 if filenames:
  for i in range(len(filenames)):
   media = wmp.newMedia(filenames[i])
   wmp.currentPlaylist.appendItem(media)
   
   print(filenames[i])
   
   coco = eyed3.load(filenames[i])#eyed3模块读取mp3信息
   total = int(coco.info.time_secs)
   minute = int(coco.info.time_secs)//60
   sec = int(coco.info.time_secs)%60
   length = int(coco.info.time_secs)
   
   name = filenames[i].split("/")
   
   i =index[-1]
   list_name.insert(END,str(i)+"."+name[-1])
   list_name.insert(END," "*6)
   if sec >=10:
    list_name.insert(END,"0%d:%d" %(minute,sec)+ "\n")
   else:
    list_name.insert(END,"0%s:0%d" %(minute,sec)+ "\n")
   i = i +1
   index.append(i)
def play(event = None):
 #root.title("%s" % name[-1]),使用wmp.currentMedia.name更好,在per函数中
 per_thread = threading.Thread(target = per)
 per_thread.daemnon = True
 wmp.controls.play()
 per_thread.start()
 #print(wmp.currentMedia.duration)#放到暂停那里居然可以用,而这里不行
 
  
def per():
 global total
 while wmp.playState !=1:
  progress_scal.set(int(wmp.controls.currentPosition))
  progress_scal.config(label = wmp.controls.currentPositionString)
  progress_scal.config(to = total,tickinterval = 50)
  time.sleep(1)
  root.title("%s" % wmp.currentMedia.name)
  
def stop():
 wmp.controls.stop()
def pause(event = None):
 wmp.controls.pause()
 
def uselist():
  pass
def fullscr():
  pass
def exitit():
 root.destroy()
def Previous_it():
 wmp.controls.previous()
def Next_it():
 wmp.controls.next()
def Volume_ctr(none):
 wmp.settings.Volume = vio_scale.get()
def Volume_add(i=[0]):
 wmp.settings.Volume =wmp.settings.Volume+5
 i.append(wmp.settings.Volume)
 vio_scale.set(wmp.settings.Volume)
def Volume_minus(i=[0]):
 wmp.settings.Volume = wmp.settings.Volume -5
 i.append(wmp.settings.Volume)
 vio_scale.set(wmp.settings.Volume)
def Scale_ctr(none):
 
 wmp.controls.currentPosition = var_scale.get()
 print(wmp.currentMedia.duration)
def Clear_list():
 wmp.currentPlaylist.clear()
 list_name.delete(1.0,END)
 name = []
 index = []
def List_random():
 wmp.settings.setMode("shuffle",True)
 play()
def List_loop():
 wmp.settings.setMode("loop",True)
 play()
  
root =Tk()
wmp = Dispatch("WMPlayer.OCX")
 
canvas = Canvas(root,width =150,height = 100,bg = "blue")
filename = PhotoImage(file = "girl.gif")
image =canvas.create_image((0,0),image = filename)
canvas.place(x=0,y=0)
canvas.coords(image,79,50)
canvas.grid(row =0,column = 0,sticky = "nw",rowspan =2)
 
 
progress_lab = LabelFrame(root,text = "播放进度")
progress_lab.grid(row =2,column =0,sticky = "we",rowspan = 2)
var_scale = DoubleVar()
progress_scal = Scale(progress_lab,orient = HORIZONTAL,showvalue = 0,length =180,variable = var_scale)
#progress_scal.bind("<Button-1>",pause)
#progress_scal.bind("")
#progress_scal.bind("<ButtonRelease-1>",play)
progress_scal.grid(row =3,column =0)
 
modee_lab = LabelFrame(root,text = "播放模式")
modee_lab.grid(row =4,column =0,rowspan =4,sticky = "ws")
var_mode = IntVar()
randomradio = Radiobutton(modee_lab,variable = var_mode,value = 1,text ="随机播放",command =List_random )
randomradio.grid(row =4,column =2)
inturnradio = Radiobutton(modee_lab,variable = var_mode,value =2,text= "顺序播放",command = play)
inturnradio.grid(row=4,column =3)
alloop = Radiobutton(modee_lab,variable = var_mode,value =2,text = "全部循环播放",command = List_loop)
alloop.grid(row =5,column = 2)
sinloop = Radiobutton(modee_lab,variable = var_mode,value =3,text = "单曲循环播放")
sinloop.grid(row =5,column =3)
previous_play = Button(modee_lab,text = "上一曲",height =1,command = Previous_it)
previous_play.grid(row =6,column =2,rowspan =2,pady =5)
next_play = Button(modee_lab,text = "下一曲",height =1,command = Next_it)
next_play.grid(row =6,column =3,rowspan =2,pady =5)
 
var_volume = IntVar()
vioce_lab = LabelFrame(root,text = "音量控制")
vioce_lab.grid(row =8,column =0,sticky = "wes")
vio_scale = Scale(vioce_lab,orient = HORIZONTAL,length =170,variable = var_volume,command =Volume_ctr)
vio_scale.set(30)
vio_scale.grid(row =8,column =0)
vio_plus = Button(vioce_lab,width =8,text = "增加音量+",command =Volume_add)
vio_plus.grid(row =9,column =0,sticky = "w")
vio_minus = Button(vioce_lab,width =8,text ="减少音量-",command = Volume_minus)
vio_minus.grid(row =9,column =0,sticky ="e")
 
ctr_lab = LabelFrame(root,text = "播放控制",height =130)
ctr_lab.grid(row =0,column =1,rowspan =12,sticky = "ns")
btn_open = Button(ctr_lab,text ="打开音乐文件",width =10,command = openfile)
btn_open.grid(row=0,column =1)
btn_play = Button(ctr_lab,text ="播放",width =10,command = play)
btn_play.grid(row =1,column =1,pady =5)
btn_stop = Button(ctr_lab,text ="停止",width =10,command = stop)
btn_stop.grid(row =2,column =1,pady =5)
btn_pause = Button(ctr_lab,text ="暂停",width =10,command = pause)
btn_pause.grid(row =3,column =1,pady =5)
 
btn_playlist = Button(ctr_lab,text ="新建播放列表",width =10,command = uselist)
btn_playlist.grid(row =4,column =1,pady =5)
 
 
listimport = Button(ctr_lab,width =10,text = "导入列表")
listimport.grid(row =6,column =1,sticky ="nw",pady =5)
listexport = Button(ctr_lab,width =10,text = "导出列表")
listexport.grid(row =7,column =1,sticky = "nw",pady =5)
listdel_all = Button(ctr_lab,width =10,text = "清空列表",command = Clear_list)
listdel_all.grid(row =8,column =1,sticky ="nw",pady =5)
listdel_sel= Button(ctr_lab,width =10,text = "删除歌曲")
listdel_sel.grid(row =12,column =1,sticky = "nw",pady =5)
savelist_btn = Button(ctr_lab,text = "保存为列表")
savelist_btn.grid(row=9,column =1)
min_btn = Button(ctr_lab,text = "最小化窗口",command = root.iconify)
min_btn.grid(row =13,column =1)
 
time_lab= Label(root,width =20,height =2,text ="现在时间为:")
time_lab.grid(row =12,column =0,sticky = "nw",pady =5)
time_text= Text(root,width =30,height =3,foreground ="green")
time_text.grid(row =10,column =0,sticky = "nw",pady =5)
 
list_name = Text(root,height =18,width =110)
list_name.grid(row =0,column =2,sticky = "n",rowspan =6)
 
 
root.mainloop()

以上就是本文的全部内容,希望对大家学习python程序设计有所帮助。

Python 相关文章推荐
python检测远程服务器tcp端口的方法
Mar 14 Python
详解Python中for循环是如何工作的
Jun 30 Python
浅谈Python用QQ邮箱发送邮件时授权码的问题
Jan 29 Python
python OpenCV学习笔记实现二维直方图
Feb 08 Python
用python生成1000个txt文件的方法
Oct 25 Python
Python文件常见操作实例分析【读写、遍历】
Dec 10 Python
10招!看骨灰级Pythoner玩转Python的方法
Apr 15 Python
PyQt5 QListWidget选择多项并返回的实例
Jun 17 Python
python中count函数简单用法
Jan 05 Python
Python字典取键、值对的方法步骤
Sep 30 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
Dec 11 Python
Python文件名匹配与文件复制的实现
Dec 11 Python
使用python3.5仿微软记事本notepad
Jun 15 #Python
python3.5仿微软计算器程序
Mar 30 #Python
Python的Asyncore异步Socket模块及实现端口转发的例子
Jun 14 #Python
Python实现TCP/IP协议下的端口转发及重定向示例
Jun 14 #Python
Python实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 #Python
浅谈python新手中常见的疑惑及解答
Jun 14 #Python
Python中死锁的形成示例及死锁情况的防止
Jun 14 #Python
You might like
如何开发一个虚拟域名系统
2006/10/09 PHP
PHP中将ip地址转成十进制数的两种实用方法
2013/08/15 PHP
preg_match_all使用心得分享
2014/01/31 PHP
php面向对象重点知识分享
2019/09/27 PHP
ANT 压缩(去掉空格/注释)JS文件可提高js运行速度
2013/04/15 Javascript
20行代码实现的一个CSS覆盖率测试脚本
2013/07/07 Javascript
jQuery创建DOM元素实例解析
2015/01/19 Javascript
Bootstrap每天必学之响应式导航、轮播图
2016/04/25 Javascript
React实现双向绑定示例代码
2016/09/19 Javascript
javascript常用的设计模式
2017/02/09 Javascript
VUE元素的隐藏和显示(v-show指令)
2017/06/23 Javascript
Angular+Bootstrap+Spring Boot实现分页功能实例代码
2017/07/21 Javascript
Vue常见面试题整理【值得收藏】
2018/09/20 Javascript
为什么JavaScript中0.1 + 0.2 != 0.3
2020/12/03 Javascript
[03:03]2014DOTA2国际邀请赛 EG战队专访
2014/07/12 DOTA
[40:48]DOTA2上海特级锦标赛D组败者赛 Liquid VS COL第二局
2016/02/28 DOTA
[37:29]完美世界DOTA2联赛PWL S2 LBZS vs Forest 第二场 11.19
2020/11/19 DOTA
Python3使用PyQt5制作简单的画板/手写板实例
2017/10/19 Python
Python中sort和sorted函数代码解析
2018/01/25 Python
[原创]Python入门教程4. 元组基本操作
2018/10/31 Python
python 模拟创建seafile 目录操作示例
2019/09/26 Python
如何使用python进行pdf文件分割
2019/11/11 Python
Python函数参数类型及排序原理总结
2019/12/19 Python
Django Haystack 全文检索与关键词高亮的实现
2020/02/17 Python
python 线性回归分析模型检验标准--拟合优度详解
2020/02/24 Python
Pandas替换及部分替换(replace)实现流程详解
2020/10/12 Python
用canvas实现图片滤镜效果附演示
2013/11/05 HTML / CSS
物流仓储实习自我鉴定
2013/09/25 职场文书
建筑实习自我鉴定
2013/10/18 职场文书
列车长先进事迹材料
2014/01/25 职场文书
学习全国两会精神心得体会范文
2014/03/17 职场文书
科长个人四风问题整改措施思想汇报
2014/10/13 职场文书
2015年幼儿园班主任工作总结
2015/05/12 职场文书
大学军训心得体会800字
2016/01/11 职场文书
2016年党员学习廉政准则心得体会
2016/01/20 职场文书
超级实用!五步法则,教你写好年终工作总结
2019/12/05 职场文书