pygame和tkinter做的音乐播放器(简单版)
# 新建一个music文件夹放入音乐(格式mp3)与py文件一起.import pygame,random,os,tkinter
window=tkinter.Tk()
window.geometry("400x150")
window.title("pp音乐")
path="music\\"
music_list=os.listdir(path)
print(music_list)
pygame.init()
pygame.mixer.init()
music_num=0
# SONG_FINISHED = pygame.USEREVENT + 1
def player(file):
stop()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
def play():
stop()
file = path + music_list
player(file)
def stop():
pygame.mixer.music.stop()
#随机播放
def shuffle_play():
stop()
global music_num
music_num=random.randint(0,len(music_list))
file=path+music_list
player(file)
def next():
stop()
global music_num
if music_num==len(music_list)-1:
music_num=0
else:
music_num+=1
file=path+music_list
player(file)
#前一首
def previous():
stop()
global music_num
if music_num==0:
music_num=len(music_list)-1
else:
music_num+=1
file=path+music_list
player(file)
# for event in pygame.event.get():
# if event.type==SONG_FINISHED:
# music_num+=1
# file = path + music_list
# player(file)
label=tkinter.Label(window,text="音乐播放器",font="黑体 28 bold",bg="gold",fg="green",height=2,width=20 )
play_bt=tkinter.Button(window,text="play",command=play,font="黑体 20 bold",bg="gold",fg="green",height=2,width=4)
stop_bt=tkinter.Button(window,text="stop",command=stop,font="黑体 20 bold",bg="gold",fg="green",height=2,width=4)
next_bt=tkinter.Button(window,text="next",command=next,font="黑体 20 bold",bg="gold",fg="green",height=2,width=4)
previous_bt=tkinter.Button(window,text="previous",command=previous,font="黑体 20 bold",bg="gold",fg="green",height=2,width=9)
label.grid(row=0,column=0,columnspan=4)
play_bt.grid(row=1,column=0,padx=10,pady=10)#padx=10边距
stop_bt.grid(row=1,column=1)
next_bt.grid(row=1,column=2)
previous_bt.grid(row=1,column=3)
shuffle_play()
window.mainloop() 本帖最后由 xyyzzjw 于 2022-7-16 08:41 编辑
ding 音乐播放器
页:
[1]