我最近闲的没事,写了一个基于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)
还能完善,大家看一看: