利用Python編寫本地音樂播放器
先上完整代碼:
修改文件夾路徑即可運(yùn)行
# -*- encoding: utf-8 -*- ''' @Description: ? ? ? : @Date ? ? :2022/03/24 17:43:26 @Author ? ? ?:驟&雨 @version ? ? ?:1.0 ''' #導(dǎo)入相關(guān)庫文件 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 ?: 音樂播放,默認(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('音樂播放器') 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è)線程來播放音樂 ? ? 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í)沒有播放音樂') ? ? 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í)沒有播放音樂!') labelName = tkinter.Label(root,textvariable=musicName) labelName.place(x=0,y=40,width=700,height=20) #啟動消息循環(huán) root.mainloop()
運(yùn)行結(jié)果如下:
到此這篇關(guān)于利用Python編寫本地音樂播放器的文章就介紹到這了,更多相關(guān)Python編寫音樂播放器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)爬蟲統(tǒng)計(jì)學(xué)校BBS男女比例(一)
這篇文章主要介紹了python實(shí)現(xiàn)爬蟲統(tǒng)計(jì)學(xué)校BBS男女比例,,需要的朋友可以參考下2015-12-12Python調(diào)用SQLPlus來操作和解析Oracle數(shù)據(jù)庫的方法
這篇文章主要介紹了Python調(diào)用SQLPlus來操作和解析Oracle數(shù)據(jù)庫的方法,這樣用SQL*Plus方式來分析Oracle中的數(shù)據(jù)就變得十分方便,需要的朋友可以參考下2016-04-04詳述numpy中的np.random.random()系列函數(shù)用法
本文主要介紹了詳述numpy中的np.random.random()系列函數(shù)用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Numpy中如何創(chuàng)建矩陣并等間隔抽取數(shù)據(jù)
這篇文章主要介紹了Numpy中如何創(chuàng)建矩陣并等間隔抽取數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05python使用cartopy在地圖中添加經(jīng)緯線的示例代碼
gridlines可以根據(jù)坐標(biāo)系,自動繪制網(wǎng)格線,這對于普通繪圖來說顯然不必單獨(dú)拿出來說說,但在地圖中,經(jīng)緯線幾乎是必不可少的,本文將給大家介紹了python使用cartopy在地圖中添加經(jīng)緯線的方法,需要的朋友可以參考下2024-01-01pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解
這篇文章主要介紹了pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Django中的CACHE_BACKEND參數(shù)和站點(diǎn)級Cache設(shè)置
這篇文章主要介紹了Django中的CACHE_BACKEND參數(shù)和站點(diǎn)級Cache設(shè)置,Python是最具人氣的Python web框架,需要的朋友可以參考下2015-07-07