Python

我最近闲的没事,写了一个基于Python的mp3播放器,
import tkinter.messagebox
from tkinter import *
import tkinter.filedialog
import pygame
import json
import time
import os
import turtle



def back():
    def go_e(event):
        pygame.mixer.music.unpause()
    def stop_e(event):
        pygame.mixer.music.pause()
    def go():
        pygame.mixer.music.unpause()
    def stop():
        pygame.mixer.music.pause()
    def main():
        os.chdir("D:\\")
        pygame.mixer.init()
        busy = pygame.mixer.music.get_busy()
        if busy == True:
            pygame.mixer.music.fadeout(1000)
        file = tkinter.filedialog.askopenfilename()
        try:
            pygame.mixer.music.load(file)
        except pygame.error:
            tkinter.messagebox.showwarning("mp3 player","please choose a mp3 file")
            pygame.mixer.init()
            file = tkinter.filedialog.askopenfilename()
            pygame.mixer.music.load(file)
            pygame.mixer.music.play()
            filename = "music log.rice"
            et = "djnejdnejkdnejkndek"
            with open(filename,"w") as obj:
                json.dump(file,obj)
            os.chdir("C:\\Users\Administrator\Desktop")
            with open("dddd.rice","w") as objj:
                json.dump(et,objj)
        filename = "music log.rice"
        et = "djnejdnejkdnejkndek"
        with open(filename,"w") as obj:
            json.dump(file,obj)
        os.chdir("C:\\Users\Administrator\Desktop")
        with open("dddd.rice","w") as objj:
            json.dump(et,objj)
        pygame.mixer.music.play()
    def helpp():
        print("""click stop to stop
    click stop to stop
    click load file and play to choose file and play""")
        tkinter.messagebox.showwarning("mp3 player -- help","""click stop to stop
    click stop to stop
    click load file and play to choose file and play""")
    def qui():
        pygame.mixer.music.fadeout(1000)
        quit()
    def loop():
        os.chdir("D:\\")
        pygame.mixer.init()
        busy = pygame.mixer.music.get_busy()
        if busy == True:
            pygame.mixer.music.fadeout(1000)
        file = tkinter.filedialog.askopenfilename()
        try:
            pygame.mixer.music.load(file)
        except pygame.error:
            tkinter.messagebox.showwarning("mp3 player","please choose a mp3 file")
            pygame.mixer.init()
            file = tkinter.filedialog.askopenfilename()
            pygame.mixer.music.load(file)
            pygame.mixer.music.play(loops=100,start=0.0)
            filename = "music log.rice"
            with open(filename,"w") as obj:
                json.dump(file,obj)
        filename = "music log.rice"
        with open(filename,"w") as obj:
            json.dump(file,obj)
        pygame.mixer.music.play(loops=100,start=0.0)
    tk = Tk()
    tk.title("mp3 player")
    tk.geometry("400x400")
    tk.bind("<KeyPress-Up>",go_e)
    tk.bind("<KeyPress-Down>",stop_e)
    btn = Button(tk,text="stop",fg="red",command=stop)
    btn.grid(row=0,column=0)
    b = Button(tk,text="go",fg="red",command=go)
    b.grid(row=1,column=0)
    bt = Button(tk,text="load file and play",fg="red",command=main)
    bt.grid(row=2,column=0)
    bn = Button(tk,text="quit",fg="red",command=qui)
    bn.grid(row=3,column=0)
    w = Button(tk,text="help",fg="red",command=helpp)
    w.grid(row=4,column=0)
    ww = Button(tk,text="loop play",fg="red",command=loop)
    ww.grid(row=5,column=0)
    c = Button(tk,text="list play",command=listplay)
    c.grid(row=6,column=0)





#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#




lis = ["list play","loop play"]
def listplay():
    t = Tk()
    t.title("list")
    t.geometry("400x400")
    
    def GUIopen():
        pygame.mixer.init()
        file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la = Label(t,text=file)
        la.grid(row=0,column=0)
        file1 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la1 = Label(t,text=file1)
        la1.grid(row=1,column=0)
        file2 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la2 = Label(t,text=file2)
        la2.grid(row=2,column=0)
        file3 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la3 = Label(t,text=file3)
        la3.grid(row=3,column=0)
        file4 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la4 = Label(t,text=file4)
        la4.grid(row=4,column=0)
        file5 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la5 = Label(t,text=file5)
        la5.grid(row=5,column=0)
        file6 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la6 = Label(t,text=file6)
        la6.grid(row=6,column=0)
        file7 = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
        la7 = Label(t,text=file7)
        la7.grid(row=7,column=0)
        pygame.mixer.music.load(file)
        pygame.mixer.music.play()
        FN = "list play music log.rice"
        os.chdir("D:\\")
        ttt = file + file1 + file2 + file3 + file4 + file5 + file6 + file7
        with open(FN,"w") as obj:
            json.dump(ttt,obj)
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
            if a == "back":
                back()
                break
        pygame.mixer.music.load(file1)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        pygame.mixer.music.load(file2)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        pygame.mixer.music.load(file3)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        pygame.mixer.music.load(file4)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        pygame.mixer.music.load(file5)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        pygame.mixer.music.load(file6)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixer.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        pygame.mixer.music.load(file7)
        pygame.mixer.music.play()
        while 1:
            busy = pygame.mixer.music.get_busy()
            if busy == False:
                break
            a = turtle.textinput("mp3 player","contral:go - unpause  stop - pause  next - next file  quit - exit program  next music - next file in list")
            if a == "go":
                pygame.mixer.music.unpause()
            if a == "stop":
                pygame.mixer.music.pause()
            if a == "next":
                os.chdir("D:\\")
                pygame.mixer.init()
                busy = pygame.mixer.music.get_busy()
                if busy == True:
                    pygame.mixe
                    r.music.fadeout(1000)
                file = tkinter.filedialog.askopenfilename(title="open mp3 file",filetypes=[("open mp3 file","*.mp3")])
                pygame.mixer.music.load(file)
                filename = "music log.rice"
                with open(filename,"w") as obj:
                    json.dump(file,obj)
                pygame.mixer.music.play()
            if a == "quit":
                quit()
            if a == "next music":
                break
        tkinter.messagebox.showwarning("mp3 player","list end,please reload file.")
    gettere = Button(t,text="GUI open",command=GUIopen)
    gettere.grid(row=0,column=2)
def go_e(event):
    pygame.mixer.music.unpause()
def stop_e(event):
    pygame.mixer.music.pause()
def go():
    pygame.mixer.music.unpause()
def stop():
    pygame.mixer.music.pause()
def main():
    os.chdir("D:\\")
    pygame.mixer.init()
    busy = pygame.mixer.music.get_busy()
    if busy == True:
        pygame.mixer.music.fadeout(1000)
    file = tkinter.filedialog.askopenfilename()
    try:
        pygame.mixer.music.load(file)
    except pygame.error:
        tkinter.messagebox.showwarning("mp3 player","please choose a mp3 file")
        pygame.mixer.init()
        file = tkinter.filedialog.askopenfilename()
        pygame.mixer.music.load(file)
        pygame.mixer.music.play()
        filename = "music log.rice"
        et = "djnejdnejkdnejkndek"
        with open(filename,"w") as obj:
            json.dump(file,obj)
        os.chdir("C:\\Users\Administrator\Desktop")
        with open("dddd.rice","w") as objj:
            json.dump(et,objj)
    filename = "music log.rice"
    et = "djnejdnejkdnejkndek"
    with open(filename,"w") as obj:
        json.dump(file,obj)
    os.chdir("C:\\Users\Administrator\Desktop")
    with open("dddd.rice","w") as objj:
        json.dump(et,objj)
    pygame.mixer.music.play()
def helpp():
    print("""click stop to stop
click stop to stop
click load file and play to choose file and play""")
    tkinter.messagebox.showwarning("mp3 player -- help","""click stop to stop
click stop to stop
click load file and play to choose file and play""")
def qui():
    pygame.mixer.music.fadeout(1000)
    quit()
def loop():
    os.chdir("D:\\")
    pygame.mixer.init()
    busy = pygame.mixer.music.get_busy()
    if busy == True:
        pygame.mixer.music.fadeout(1000)
    file = tkinter.filedialog.askopenfilename()
    try:
        pygame.mixer.music.load(file)
    except pygame.error:
        tkinter.messagebox.showwarning("mp3 player","please choose a mp3 file")
        pygame.mixer.init()
        file = tkinter.filedialog.askopenfilename()
        pygame.mixer.music.load(file)
        pygame.mixer.music.play(loops=100,start=0.0)
        filename = "music log.rice"
        with open(filename,"w") as obj:
            json.dump(file,obj)
    filename = "music log.rice"
    with open(filename,"w") as obj:
        json.dump(file,obj)
    pygame.mixer.music.play(loops=100,start=0.0)
tk = Tk()
tk.title("mp3 player")
tk.geometry("400x400")
tk.bind("<KeyPress-Up>",go_e)
tk.bind("<KeyPress-Down>",stop_e)
btn = Button(tk,text="stop",fg="red",command=stop)
btn.grid(row=0,column=0)
b = Button(tk,text="go",fg="red",command=go)
b.grid(row=1,column=0)
bt = Button(tk,text="load file and play",fg="red",command=main)
bt.grid(row=2,column=0)
bn = Button(tk,text="quit",fg="red",command=qui)
bn.grid(row=3,column=0)
w = Button(tk,text="help",fg="red",command=helpp)
w.grid(row=4,column=0)
ww = Button(tk,text="loop play",fg="red",command=loop)
ww.grid(row=5,column=0)
c = Button(tk,text="list play",command=listplay)
c.grid(row=6,column=0)


还能完善,大家看一看:
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值