當(dāng)時(shí)學(xué)了半個(gè)月python,有一天突然心血來(lái)潮做的音樂(lè)播放器蚕捉,這樣就可以用自己的播放器聽(tīng)周杰倫的歌了W喔荨!迫淹!
說(shuō)說(shuō)我的界面布置吧秘通。聽(tīng)歌我是用了兩種想法,因?yàn)橹饕遣シ疟镜匾魳?lè)敛熬,一種是選擇文件播放充易,另一種是隨機(jī)播放。壁紙的原理和選擇文件類似荸型。然后就是播放器的常規(guī)界面盹靴,顯示播放歌曲炸茧,歌曲時(shí)間(有點(diǎn)low),還有暫停稿静,繼續(xù)播放梭冠,下一首,結(jié)束播放改备,音量控制控漠。提醒功能的想法是每隔兩個(gè)小時(shí)文字提醒要起來(lái)休息并且提醒一分鐘后放歌(要是兩個(gè)搞反可能會(huì)被嚇到)提醒右邊的是虛化邊框,個(gè)人感覺(jué)很帥氣悬钳。最下面加了實(shí)時(shí)的時(shí)間盐捷。
安裝python注意點(diǎn):
1. 要在開(kāi)始的界面勾選Add python 3.x to PATH,這樣就不需要自己去配置環(huán)境變量;
2. 要在自定義安裝中勾選install for all users;
3. 在安裝成功的結(jié)束界面可能會(huì)出現(xiàn)Disable path length limit的按鈕,有點(diǎn)話點(diǎn)一下就好了;
音樂(lè)播放器需要導(dǎo)入的庫(kù)有
- tkinter
- re
- threading
- pygame
- time?
- os
-? random
**安裝庫(kù)方法**
1. 打開(kāi)cmd中python環(huán)境
2. pip install 庫(kù)名
下面是音樂(lè)播放器的代碼,可能當(dāng)中還存在諸多的問(wèn)題默勾。
import tkinter
from tkinter import Button
from tkinter import Label
from tkinter import Entry
from tkinter import Scale
from tkinter import Label,PhotoImage
from PIL import Image,ImageTk
from tkinter import messagebox
from tkinter import Toplevel
from pymediainfo import MediaInfo
import re
from tkinter import Message
import threading
import pygame
import time
import os
import random
from tkinter.filedialog? import askopenfilename
from tkinter.filedialog import askdirectory
from tkinter import StringVar
top=tkinter.Tk()
top.geometry("800x400")
top.title("嘟嘟的音樂(lè)播放器")
def printsrceen(texts):
? ? t=int(texts)
? ? top.attributes("-alpha",t/100)
screenwidth = top.winfo_screenwidth()
screenheight = top.winfo_screenheight() - 100
pygame.init()
path=StringVar()
paths=StringVar()
patht=StringVar()
v=StringVar()
v1=StringVar()
def callback():#搜索本地文件
? ? path_= askopenfilename()
? ? return path_
def selectPath():#隨機(jī)播放
? ? folder_path="D:/音樂(lè)"
? ? folder_list = os.listdir(folder_path)#遍歷文件夾里面每個(gè)文件
? ? list=[]
? ? count=0
? ? for i in folder_list:#將文件夾里的文件按順序傳提給變量i? 此處區(qū)別os.walk()
? ? ? ? if os.path.splitext(i)[1]=='.flac':#提取特定后綴文件'.***'
? ? ? ? ? ? list.append (i)
? ? ? ? #print(type(list))
? ? ? ? ? ? count=count+1
? ? #print(count)
? ? s=random.randint(0,(count-1))#獲取隨機(jī)數(shù)
? ? file=list[s]
? ? fil=folder_path+"\\"+file
? ? pygame.mixer.music.load(fil)
? ? pygame.mixer.music.play(1,0)
? ? media_info = MediaInfo.parse(fil)
? ? data = media_info.to_json()#medio到j(luò)son()這兩行是獲取文件的所有屬性
? ? rst=re.search('other_duration.*?(.*?)min(.*?)s.*?',data)
? ? t=int(rst.group(0)[19:20])
? ? r=int(rst.group(0)[-4:-2])
? ? m=(t*60+r)*1000
? ? musictime=str(t)+':'+str(r)
? ? l2.config(text=file)
? ? l3.config(text=musictime)
? ? lbTime=tkinter.Label(top,anchor='w')
? ? lbTime.place(x=25,y=150)
? ? def autoclose():
? ? ? ? for i in range(m//1000):
? ? ? ? ? ? lbTime['text']='-{} /'.format((m//1000)-i)
? ? ? ? ? ? time.sleep(1)
? ? t=threading.Thread(target=autoclose)
? ? t.start()
? ? loopl=top.after(m,selectPath)
def printScale(text):
? ? t=int(text)
? ? pygame.mixer.music.set_volume(t/100)
def update_timeText():
? ? # Get the current time, note you can change the format as you wish
? ? current = time.strftime("%H:%M:%S")#獲取當(dāng)前時(shí)間
# Update the timeText Label box with the current time
? ? timeText.configure(text=current)
# Call the update_timeText() function after 1 second
? ? top.after(1000, update_timeText)
def remind():
? ? top = Toplevel()#新建一個(gè)tkinter窗口
? ? top.title('使用提示')
? ? top.geometry("200x200")
? ? t="半分鐘后開(kāi)始播放音樂(lè)"
? ? msg = Message(top,text = t)
? ? msg.config( font=('times', 18, 'italic'))
? ? msg.place(x=0,y=0)
? ? lbTime=tkinter.Label(top,fg="red",anchor='w')
? ? lbTime.place(x=100,y=45)
? ? def autoclose():
? ? ? ? for i in range(30):
? ? ? ? ? ? lbTime['text']='距離窗口關(guān)閉還有{}秒'.format(30-i)
? ? ? ? ? ? time.sleep(1)
? ? ? ? top.destroy()
? ? t=threading.Thread(target=autoclose)
? ? t.start()
? ? loopl=top.after(60*59500,remind)
def reminds():
? ? top = Toplevel()
? ? top.title('使用提示')
? ? top.geometry("200x200")
? ? t="寶貝可以休息一會(huì)啦"
? ? msg = Message(top,text = t)
? ? msg.config( font=('times', 24, 'italic'))
? ? msg.place(x=0,y=0)
? ? folder_path="D:/音樂(lè)"
? ? folder_list = os.listdir(folder_path)#遍歷文件夾里面每個(gè)文件
? ? list=[]
? ? count=0
? ? for i in folder_list:#將文件夾里的文件按順序傳提給變量i? 此處區(qū)別os.walk()
? ? ? ? if os.path.splitext(i)[1]=='.flac':#提取特定后綴文件'.***'
? ? ? ? ? ? list.append (i)
? ? ? ? #print(type(list))
? ? ? ? ? ? count=count+1
? ? ? ? #print(count)
? ? s=random.randint(0,(count-1))
? ? file=list[s]
? ? fil=folder_path+"\\"+file
? ? pygame.mixer.music.load(fil)
? ? pygame.mixer.music.play(1,0)
? ? lbTime=tkinter.Label(top,fg="red",anchor='w')
? ? lbTime.place(x=100,y=45)
? ? def autoclose():
? ? ? ? for i in range(300):
? ? ? ? ? ? lbTime['text']='距離窗口關(guān)閉還有{}秒'.format(300-i)
? ? ? ? ? ? time.sleep(1)
? ? ? ? top.destroy()
? ? t=threading.Thread(target=autoclose)
? ? t.start()
? ? loopl=top.after(60*60000,reminds)
def play():#播放音樂(lè)
? ? f=callback()#選擇制定文件
? ? pygame.mixer.music.load(f)
? ? pygame.mixer.music.play()
? ? path.set(f)
? ? media_info = MediaInfo.parse(f)
? ? data = media_info.to_json()#medio到j(luò)son()這兩行是獲取文件的所有屬性
? ? rst=re.search('other_duration.*?(.*?)min(.*?)s.*?',data)
? ? t=int(rst.group(0)[19:20])
? ? r=int(rst.group(0)[-4:-2])
? ? m=(t*60+r)*1000
? ? musictime=str(t)+':'+str(r)
? ? l2.config(text=f)
? ? l3.config(text=musictime)
? ? lbTime=tkinter.Label(top,anchor='w')
? ? lbTime.place(x=25,y=150)
? ? def autoclose():
? ? ? ? for i in range(m//1000):
? ? ? ? ? ? lbTime['text']='-{} /'.format((m//1000)-i)
? ? ? ? ? ? time.sleep(1)
? ? t=threading.Thread(target=autoclose)
? ? t.start()
? ? loopl=top.after(m,selectPath)
def stop():
? ? pygame.mixer.music.stop()#停止播放
? ? top.after_cancel(loopl)
def pause():
? ? pygame.mixer.music.pause()#暫停?
def unpause():
? ? pygame.mixer.music.unpause()#繼續(xù)播放?
def choosepic():#保存的路徑不能有中文碉渡,若需要中文則吧/換成\
? ? path_s=askopenfilename()
? ? paths.set(path_s)
? ? img_open=Image.open(e1.get())
? ? img=ImageTk.PhotoImage(img_open)
? ? l1.config(image=img)
? ? l1.image=img
def create():
? ? top = Toplevel()
? ? top.title('使用提示')
? ? top.geometry("400x400")
? ? t="關(guān)于照片,新建一個(gè)存放圖片的文件母剥,用英文命名滞诺,然后存里面的圖片也用英文命名。關(guān)于音樂(lè): 新建一個(gè)名字叫音樂(lè)的文件环疼,把歌曲添加到該文件夾习霹。"
? ? msg = Message(top,text = t)
? ? msg.config( font=('times', 24, 'italic'))
? ? msg.place(x=0,y=0)
def loop():
? ? top.after(60*60000,reminds)
? ? top.after(60*59500,remind)
def loops():
? ? selectPath()
def gettime():
? ? t=time.strftime('%H%M%S')
? ? s=int(t[0:2])
? ? d=int(t[2:4])
? ? f=int(t[4:6])
? ? g=s*60*60+d*60+f
? ? return g? ?
errmsg = 'Error!'
#時(shí)間
timeText = Label(top, text="", font=("Helvetica", 15))
timeText.place(x=180,y=370)
update_timeText()
#選擇文件
Button(top,text="選擇文件/播放",command=play,width=10,bg="sky blue").place(x=20,y=20)
Entry(top,text=path,width=25,state='readonly').place(x=120,y=20)
#選擇圖片
Button(top,text='選擇圖片', command=choosepic,width=10,bg="sky blue").place(x=20,y=55)
e1=Entry(top,text=paths,state='readonly',width=25)
e1.place(x=120,y=55)
l1=Label(top)#圖片放置位置
l1.place(x=320,y=0)
#隨機(jī)播放
Button(top,text="隨機(jī)播放",command=selectPath,width=7,bg="sky blue").place(x=20,y=225)
l2=Label(top,text='',width=25,font=("Helvetica", 16))#音樂(lè)名
l2.place(x=0,y=100)
Button(top,text="下一首",command=loops,width=5,bg="sky blue").place(x=100,y=225)
l3=Label(top,text='',width=15)#音樂(lè)時(shí)長(zhǎng)
l3.place(x=24,y=150)
#暫停,繼續(xù)播放炫隶,結(jié)束播放
Button(top,text="暫停",command=pause,width=7,bg="sky blue").place(x=170,y=245)
Button(top,text="繼續(xù)播放",command=unpause,width=7,bg="sky blue").place(x=170,y=205)
Button(top,text="結(jié)束播放",command=stop,width=7,bg="sky blue").place(x=240,y=225)
#提醒功能
Button(top,text='提醒功能', command=loop,width=10,bg="sky blue").place(x=20,y=325)
#使用說(shuō)明
Button(top,text="使用說(shuō)明",command = create,width=10,bg="sky blue").place(x=20,y=370)
#音量
w1 = Scale(top, from_=0,to=100, orient="horizontal",length=75,variable=v,command=printScale,label="音量")
w1.place(x=240,y=145)
w2 = Scale(top, from_=30,to=100, orient="horizontal",length=100,variable=v1,command=printsrceen,label="透明度")
w2.place(x=150,y=290)
top.mainloop()
關(guān)于打包成exe文件的問(wèn)題
打包成exe文件步驟:
1. 先把代碼保存在C:\Users\ASUS里新建一個(gè)文件夾保存的文件后綴 .py
重點(diǎn)保存代碼后綴是 .py
2. 然后在cmd的python環(huán)境中輸入(二選一)
?pyinstaller -F -W C:\Users\ASUS\文件名 .py
?pyinstaller -F -W 文件名 .py
- -F是只有一個(gè)exe帶圖標(biāo)文件
- -W是消除命令行窗口
- -F? onefile 打包成一個(gè)exe文件淋叶。
- -D? onedir 創(chuàng)建一個(gè)目錄,包含exe文件伪阶,但會(huì)依賴很多文件(默認(rèn)選項(xiàng))爸吮。
- -C? console, –nowindowed 使用控制臺(tái),無(wú)界面(默認(rèn)選項(xiàng))
- -w windowed, –noconsole 使用窗口望门,無(wú)控制臺(tái)
如果直接pyinstaller 文件名 .py,沒(méi)有-F和-W的話,打開(kāi)的exe的播放器會(huì)帶有黑黑的命令行窗口,而且文件夾會(huì)有很多沒(méi)有用文件占內(nèi)存形娇。所以建議加上-F和-W.
最后打包好的文件路徑:C:\Users\ASUS\dist里面找,有的話你就是成功了