利用Python編寫(xiě)本地音樂(lè)播放器
先上完整代碼:
修改文件夾路徑即可運(yùn)行
# -*- encoding: utf-8 -*-
'''
@Description: ? ? ? :
@Date ? ? :2022/03/24 17:43:26
@Author ? ? ?:驟&雨
@version ? ? ?:1.0
'''
#導(dǎo)入相關(guān)庫(kù)文件
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
#設(shè)置文件夾路徑
folder = r'C:\Users\Administrator\Desktop\Python\Python Code\TiquMusicFromVedio'
"""
@description ?: 音樂(lè)播放,默認(rèn)播放文件夾內(nèi)的所有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)
? ? #初始化混音器設(shè)備
? ? pygame.mixer.init()
? ? while playing:
? ? ? ? if not pygame.mixer.music.get_busy():
? ? ? ? ? ? #隨機(jī)播放一首歌曲
? ? ? ? ? ? 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('音樂(lè)播放器')
root.geometry('700x80+400+300')
root.resizable(False,False)
#關(guān)閉程序時(shí)執(zhí)行的代碼
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
? ? #創(chuàng)建一個(gè)線(xiàn)程來(lái)播放音樂(lè)
? ? t = threading.Thread(target=play)
? ? t.start()
? ? #根據(jù)情況禁用或啟用相應(yīng)按鈕
? ? 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('暫時(shí)沒(méi)有播放音樂(lè)')
? ? 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'
#暫停與恢復(fù) 復(fù)用按鈕
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='暫時(shí)沒(méi)有播放音樂(lè)!')
labelName = tkinter.Label(root,textvariable=musicName)
labelName.place(x=0,y=40,width=700,height=20)
#啟動(dòng)消息循環(huán)
root.mainloop()運(yùn)行結(jié)果如下:

到此這篇關(guān)于利用Python編寫(xiě)本地音樂(lè)播放器的文章就介紹到這了,更多相關(guān)Python編寫(xiě)音樂(lè)播放器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python微信公眾號(hào)之關(guān)注公眾號(hào)自動(dòng)回復(fù)
這篇文章主要為大家詳細(xì)介紹了python微信公眾號(hào)之關(guān)注公眾號(hào)自動(dòng)回復(fù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Python爬蟲(chóng)代理IP池實(shí)現(xiàn)方法
在公司做分布式深網(wǎng)爬蟲(chóng),搭建了一套穩(wěn)定的代理池服務(wù),為上千個(gè)爬蟲(chóng)提供有效的代理,保證各個(gè)爬蟲(chóng)拿到的都是對(duì)應(yīng)網(wǎng)站有效的代理IP,從而保證爬蟲(chóng)快速穩(wěn)定的運(yùn)行,所以就想利用一些免費(fèi)的資源搞一個(gè)簡(jiǎn)單的代理池服務(wù)。2017-01-01
使用TensorBoard進(jìn)行超參數(shù)優(yōu)化的實(shí)現(xiàn)
這篇文章主要介紹了使用TensorBoard進(jìn)行超參數(shù)優(yōu)化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
簡(jiǎn)單有效上手Python3異步asyncio問(wèn)題
這篇文章主要介紹了簡(jiǎn)單有效上手Python3異步asyncio問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python使用eval函數(shù)解析和執(zhí)行字符串
在Python中,eval函數(shù)是一個(gè)非常強(qiáng)大的函數(shù),它可以將字符串作為代碼進(jìn)行解析和執(zhí)行,本文主要介紹了如何使用eval函數(shù)解析和執(zhí)行字符串,需要的可以了解下2024-01-01
Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文
今天小編就為大家分享一篇Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Python函數(shù)式編程實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Python函數(shù)式編程實(shí)現(xiàn)登錄注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

