Python本地音乐播放器--第二代-完善

解决了程序关闭后仍有音乐。

主要加了一个 on_closing 函数和sys库。

sys.exit()这个函数是通过抛出异常的方式来终止后台进程,更彻底。

import tkinter as tk
import tkinter.messagebox
from tkinter import filedialog
from tkinter import *
import random
from tinytag import TinyTag
import pygame
import os
import sys


def on_closing():
    sys.exit()

def show(event):
    pygame.mixer.init()
    h = float(var.get())
    vf = round(var.get() * 100)
    s = '音量为' + str(vf) + '%'
    lb.config(text=s)
    pygame.mixer.music.set_volume(h)


def suach():
    global duration, j, v
    v = e.get()
    pygame.mixer.init()
    try:
        pygame.mixer.music.load(path + v)
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.load(path + v)

        def get_duration_mp3_and_wav(file_path):
            tag = TinyTag.get(file_path)
            duration = tag.duration
            return duration

        if __name__ == "__main__":
            file_path = path1 + '/' + v
            duration = round(get_duration_mp3_and_wav(file_path))
            j = round(pygame.mixer.music.get_pos())
            s["text"] = duration
        y["text"] = "已搜索到"


def go():
    try:
        pygame.mixer.music.play()
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.play()


def reter():
    try:
        pygame.mixer.music.unpause()
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.unpause()


def stop():
    try:
        pygame.mixer.music.pause()
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.pause()


def list_1():
    global data_names
    data_names = os.listdir(path)
    top = Tk()
    sb = Scrollbar(top)
    sb.pack(side=RIGHT, fill=Y)
    mylist = Listbox(top, yscrollcommand=sb.set)
    for i in data_names:
        data_names_path = os.path.join(path, i)
        if 'mp3' or 'wav' in data_names_path:
            mylist.insert(END, str(i))
    mylist.pack(side=LEFT)
    sb.config(command=mylist.yview)
    mainloop()


def ago():
    er = e.get()
    gp5 = data_names.index(er)
    gp4 = gp5 - 1
    v = data_names[gp4]
    e.delete(0, "end")
    e.insert(0, v)
    pygame.mixer.init()
    try:
        pygame.mixer.music.load(path + v)
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.load(path + v)

        def get_duration_mp3_and_wav(file_path):
            tag = TinyTag.get(file_path)
            duration = tag.duration
            return duration

        if __name__ == "__main__":
            file_path = path1 + '/' + v
            duration = round(get_duration_mp3_and_wav(file_path))
            j = round(pygame.mixer.music.get_pos())
            s["text"] = duration
        y["text"] = "已搜索到"
        pygame.mixer.music.play()


def next_1():
    er = e.get()
    gp5 = data_names.index(er)
    gp4 = gp5 + 1
    v = data_names[gp4]
    e.delete(0, "end")
    e.insert(0, v)
    pygame.mixer.init()
    try:
        pygame.mixer.music.load(path + v)
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.load(path + v)

        def get_duration_mp3_and_wav(file_path):
            tag = TinyTag.get(file_path)
            duration = tag.duration
            return duration

        if __name__ == "__main__":
            file_path = path1 + '/' + v
            duration = round(get_duration_mp3_and_wav(file_path))
            s["text"] = duration
        y["text"] = "已搜索到"
        pygame.mixer.music.play()


def redom():
    global v
    fg = len(data_names)
    gp4 = random.randint(0, fg)
    v = data_names[gp4]
    e.delete(0, "end")
    e.insert(0, v)
    pygame.mixer.init()
    try:
        pygame.mixer.music.load(path + v)
    except Exception:
        y["text"] = "输入错误"
    else:
        pygame.mixer.music.load(path + v)

        def get_duration_mp3_and_wav(file_path):
            tag = TinyTag.get(file_path)
            duration = tag.duration
            return duration

        if __name__ == "__main__":
            file_path = path1 + '/' + v
            duration = round(get_duration_mp3_and_wav(file_path))
            j = round(pygame.mixer.music.get_pos())
            s["text"] = duration
        y["text"] = "已搜索到"
        pygame.mixer.music.play()


q = tk.Tk()
q.geometry('270x300')
q.title('music Player')
q.resizable(False, False)
q.protocol("WM_DELETE_WINDOW", on_closing)
root = tk.Tk()
root.withdraw()
path1 = filedialog.askdirectory()
tkinter.messagebox.showinfo("开始", "正在启动")
path1.replace('/', '//')
path = path1 + '//'
data_names = os.listdir(path)
v = data_names[0]
e = tk.Entry(q)
e.place(x=30, y=20)
e.insert(0, v)
w = tk.Button(q, text="搜索", command=suach)
w.place(x=210, y=15)
t = tk.Button(q, text="播放", command=go)
t.place(x=20, y=55)
i = tk.Button(q, text="恢复", command=reter)
i.place(x=60, y=55)
y = tk.Button(q, text="暂停", command=stop)
y.place(x=100, y=55)
u = tk.Label(q, text="Listen to the music")
u.place(x=150, y=60)
s = tk.Label(q, text="000")
s.place(x=20, y=100)
a = tk.Label(q, text="秒")
a.place(x=44, y=100)
y = tk.Label(q, text="")
y.place(x=80, y=100)
rt = tk.Button(q, text="查看文件", command=list_1)
rt.place(x=180, y=90)
yu = tk.Button(q, text="上一个", command=ago)
yu.place(x=20, y=130)
yu = tk.Button(q, text="下一个", command=next_1)
yu.place(x=200, y=130)
yu = tk.Button(q, text="随机播放", command=redom)
yu.place(x=105, y=130)
var = DoubleVar()
scl = Scale(q, orient=HORIZONTAL, length=200, from_=0.00, to=1.00, label='请拖动滑块以调节音量', tickinterval=1,
            resolution=0.01, variable=var)
scl.bind('<ButtonRelease-1>', show)
scl.pack(side='bottom')
lb = Label(q, text='')
lb.place(x=180, y=168)
lf = Label(q, text='')
lf.place(x=45, y=168)
q.mainloop()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值