完善音乐下载器时找到一个音乐播放器,自己完善了下
软件原作者:嘟嘟还没长大
完善内容
代码
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 json
top = 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() - 100
pygame.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 = 0
for i in folder_list:
if os.path.splitext(i)[1] == '.flac':
list.append(i)
count = count + 1
s = random.randint(0, (count - 1))
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()
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():
current = time.strftime("%H:%M:%S")
timeText.configure(text=current)
top.after(1000, update_timeText)
def remind():
top = Toplevel()
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 = 0
for i in folder_list:
if os.path.splitext(i)[1] == '.flac':
list.append(i)
count = count + 1
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'] = '距离窗口关闭还有{}秒'.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()
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()
def refleshpic():
global pic_copyright
bing_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.content
f = 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 = img
def 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 + f
return g
errmsg = '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()
refleshpic()
top.mainloop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)