鱼C论坛

 找回密码
 立即注册
查看: 985|回复: 2

[原创] pygame和tkinter做的音乐播放器(简单版)

[复制链接]
发表于 2022-7-15 10:13:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# 新建一个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[music_num]
    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[music_num]
    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[music_num]
    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[music_num]
    player(file)

# for event in pygame.event.get():
#     if event.type==SONG_FINISHED:
#         music_num+=1
#         file = path + music_list[music_num]
#         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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-7-16 08:39:28 | 显示全部楼层
本帖最后由 xyyzzjw 于 2022-7-16 08:41 编辑

ding
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-7-16 08:48:06 | 显示全部楼层
音乐播放器
VeryCapture_20220716083804.png

音乐播放器.rar

887 Bytes, 下载次数: 1

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 09:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表