音乐播放器
完善音樂下載器時找到一個音樂播放器,自己完善了下
軟件原作者:嘟嘟還沒長大
完善內容
- 支持文件拖動打開
- 支持自動獲取必應每日一圖并顯示
- 隨機播放支持選擇路徑
代碼
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 sys import random from tkinter.filedialog import askopenfilename from tkinter.filedialog import askdirectory from tkinter import StringVar import requests import jsontop = tkinter.Tk() top.geometry("800x400") top.title("嘟嘟的音樂播放器") top.state("zoomed")def printsrceen(texts):t = int(texts)top.attributes("-alpha", t / 100)screenwidth = top.winfo_screenwidth() screenheight = top.winfo_screenheight() - 100pygame.init() path = StringVar() paths = StringVar() patht = StringVar() v = StringVar() v1 = StringVar()def callback(): # 搜索本地文件path_ = askopenfilename()return path_def selectPath(): # 隨機播放folder_path = "D:/音樂"folder_list = os.listdir(folder_path) # 遍歷文件夾里面每個文件list = []count = 0for i in folder_list: # 將文件夾里的文件按順序傳提給變量i 此處區別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 + "\\" + filepygame.mixer.music.load(fil)pygame.mixer.music.play(1, 0)media_info = MediaInfo.parse(fil)data = media_info.to_json() # medio到json()這兩行是獲取文件的所有屬性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) * 1000musictime = 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 wishcurrent = time.strftime("%H:%M:%S") # 獲取當前時間# Update the timeText Label box with the current timetimeText.configure(text=current)# Call the update_timeText() function after 1 secondtop.after(1000, update_timeText)def remind():top = Toplevel() # 新建一個tkinter窗口top.title('使用提示')top.geometry("200x200")t = "半分鐘后開始播放音樂"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'] = '距離窗口關閉還有{}秒'.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 = "寶貝可以休息一會啦"msg = Message(top, text=t)msg.config(font=('times', 24, 'italic'))msg.place(x=0, y=0)folder_path = "D:/音樂"folder_list = os.listdir(folder_path) # 遍歷文件夾里面每個文件list = []count = 0for i in folder_list: # 將文件夾里的文件按順序傳提給變量i 此處區別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 + "\\" + filepygame.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'] = '距離窗口關閉還有{}秒'.format(300 - i)time.sleep(1)top.destroy()t = threading.Thread(target=autoclose)t.start()loopl = top.after(60 * 60000, reminds)def play(): # 播放音樂# 拖動音樂文件到程序圖標,然后播放音樂if len(sys.argv) >= 2: # 判斷是否通過拖動音頻文件打開程序f = sys.argv[1] # 如果是,則播放拖動的文件else: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到json()這兩行是獲取文件的所有屬性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) * 1000musictime = 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() # 繼續播放def refleshpic(): # 保存的路徑不能有中文,若需要中文則吧/換成\global pic_copyrightbing_api_res = requests.get("https://api.no0a.cn/api/bing/0")bing_api_dic = bing_api_res.json()print(bing_api_dic)pic_url = bing_api_dic['bing']['url']pic_copyright = bing_api_dic['bing']['copyright']pic_res = requests.get(pic_url)pic=pic_res.contentf = open("./bing_img_cache.png", 'wb')f.write(pic)f.close()path_s = "./bing_img_cache.png"paths.set(path_s)img_open = Image.open(e1.get())img = ImageTk.PhotoImage(img_open)l1.config(image=img)l1.image = imgdef create():top = Toplevel()top.title('使用提示')top.geometry("400x400")t = "關于照片,新建一個存放圖片的文件,用英文命名,然后存里面的圖片也用英文命名。關于音樂: 新建一個名字叫音樂的文件,把歌曲添加到該文件夾。"msg = Message(top, text=t)msg.config(font=('times', 24, 'italic'))msg.place(x=0, y=0)def copyright():top = Toplevel()top.title('版權信息')top.geometry("500x400")t = """軟件原作者為 嘟嘟還沒長大 由 人工智障 完善 圖片來自 必應:每日一圖 本圖版權信息 %s"""%(pic_copyright)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 + freturn gerrmsg = 'Error!' # 時間 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=refleshpic, 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)# 隨機播放 Button(top, text="隨機播放", command=selectPath, width=7, bg="sky blue").place(x=20, y=225) l2 = Label(top, text='', width=25, font=("Helvetica", 16)) # 音樂名 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) # 音樂時長 l3.place(x=24, y=150) # 暫停,繼續播放,結束播放 Button(top, text="暫停", command=pause, width=7, bg="sky blue").place(x=170, y=245) Button(top, text="繼續播放", command=unpause, width=7, bg="sky blue").place(x=170, y=205) Button(top, text="結束播放", 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) # 使用說明 Button(top, text="使用說明", command=create, width=10, bg="sky blue").place(x=20, y=370) #版權信息 Button(top, text="版權信息", command=copyright, width=10, bg="sky blue").place(x=20, y=415) # 音量 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)if len(sys.argv) >= 2: # 初步判斷是否通過拖動音頻文件打開程序play() # 如果是,則直接運行play()函數refleshpic()top.mainloop()總結
- 上一篇: 帆软报表决策系统自定义登录界面 使用验证
- 下一篇: Java密码学原型算法实现——第三部分: