Python极其简易音乐播放器

#导入相应模块
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from pygame.locals import *
import time
import pygame
import sys


#初始化
pygame.init()

#设置用于播放歌曲的列表
fileslist = []  

def play():
    """ 播放歌曲 """
    pygame.mixer.music.unpause()

def pause():
    """ 暂停播放 """
    pygame.mixer.music.pause()

def stop():
    """ 停止播放 """
    pygame.mixer.music.stop()

def opensong():
    """ 打开歌曲路径 """
    filessonglist = filedialog.askopenfilenames() #打开多个文件
    if not filessonglist: #判断是否添加曲库
        messagebox.showwarning("音乐","当前未选择歌曲")
        return
    for item in filessonglist: #添加到播放列表当中
        fileslist.append(item)
        listname = item.split('/')
        listsong.insert(END,listname[len(listname)-1])
        
def quit():
    """ 关闭窗口 """
    root.quit()

def playcurrentsong(*args):
    """ 播放当前列表歌曲 """
    indexs = listsong.curselection()
    selectindex = int(indexs[0])
    pygame.mixer.music.load(fileslist[selectindex])
    pygame.mixer.music.play()

def frontsong():
    """ 上一首 """

def nextsong():
    """ 下一首 """

root = Tk() #创建窗口
root.Color = "red"
root.title("YTouchMusic") #标题
root.geometry('250x440') #设置窗口大小和位置
listsong = Listbox(root) #添加歌曲列表
listsong.pack(padx=5, pady=10, side=LEFT) #将列表放在左侧
listsong.bind("<<ListboxSelect>>",playcurrentsong)#点击列表,播放对应歌曲
'''menu = Menu()
me = Menu()#一级菜单
root.config(menu=me)#加入一级菜单
'''
root.attributes('-toolwindow', False, 
                '-alpha', 0.9, #设置透明度
                '-topmost', True)
pygame.init()
pygame.mixer.init()
l = Label(root, text="YTouch - why")
l.pack()

'''相关button '''
btn_ChooseMusic = Button(root,text = "选择文件",command = opensong)
btn_ChooseMusic.pack(padx=1, pady=12, side=RIGHT)
btn_Pause =Button(root,text = "暂停播放",command = pause)
btn_Pause.pack()
btn_Continue=Button(root,text = "继续",command = play)
btn_Continue.pack()
btn_Stop =Button(root,text = "停止",command = stop)
btn_Stop.pack()
btn_Quit =Button(root,text = "关闭",command = quit)
btn_Quit.pack()

root.mainloop() #必需组件

本人小白  刚学两天的python 所以做的很水  以后慢慢完善

效果如下:

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Music 爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值