大蟒蛇python教程共享利用Python编写本地音乐播放器

先上完整代码:

修改文件夹路径即可运行

# -*- encoding: utf-8 -*-  '''  @description:       :  @date     :2022/03/24 17:43:26  @author      :骤&雨  @version      :1.0  '''  #导入相关库文件  import os  import tkinter  import tkinter.filedialog  import random  import time  import threading  import pygame    from asyncio.base_tasks import _task_print_stack  from cprofile import label  from email import header  from functools import total_ordering  from importlib import find_loader  from logging import root  from mimetypes import init  from re import x  from turtle import width  from matplotlib.pyplot import pause  from scipy import rand    #设置文件夹路径  folder = r'c:usersadministratordesktoppythonpython codetiqumusicfromvedio'      """  @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编写本地音乐播放器

到此这篇关于利用python编写本地音乐播放器的文章就介绍到这了,更多相关python编写音乐播放器内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多python教程分享利用Python编写本地音乐播放器,都可以关注python教程分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/pythontutorial/1069140.html

(0)
上一篇 2022年3月28日
下一篇 2022年3月28日

精彩推荐