用 Python 创建属于自己的网易云音乐
Tkinter
:用于UIPygame
:播放音乐os
:用于访问系统文件
pip install pygame
pip install tkinter
os
已经安装好了,所以我们只需要导入它即可:from tkinter import *
from pygame import mixer
import os
root = Tk()
root.title(“music player”)
Button(root,text= “play”, command = playsong).grid(row =1 , column = 0)
Button(root,text= “pause”, command = pausesong).grid(row =1 , column = 1)
Button(root,text= “stop”, command = stopsong).grid(row =1 , column = 2)
Button(root,text= “resume”, command = resumesong).grid(row =1 , column = 3)
mainloop()
mixer.init()
playlist = Listbox(root,selectmode=SINGLE,bg = “green”)
playlist.grid(columnspan =5)
os.chdir(‘/Users/rajatupadhyaya/Documents/song’)
song = os.listdir()
for s in song:
playlist.insert(END,s)
mixer.init()
将检查系统中所有当前的歌曲。在下一行中,我们创建了一个列表框,该列表框将显示在我们的UI中。os
在下一行没有帮助访问特定目录,因此我们给出了路径并将所有音乐存储在歌曲变量中,并运行了一个将所有歌曲循环存储在播放列表中。现在,我们将在最后一步定义功能并创建播放列表。def playsong():
currentsong = playlist.get(ACTIVE)
print(currentsong)
mixer.music.load(currentsong)
mixer.music.play()
def pausesong():
mixer.music.pause()
def stopsong():
mixer.music.stop()
def resumesong():
mixer.music.unpause()
playsong
函数将检查当前选择的歌曲并加载歌曲后播放。点击下方阅读原文加入社区会员
评论