|
音乐播放器代码(python的tkinter以及pygame自制音乐播放器的代码解析)
1.阐明:
1.1 保举环境:python3.8八、微软vscode编纂器八、pygame模块
1.2 认识tkinter的相干结构,gif导入八、图片导入八、路径文件导入,音乐播放以及按钮八、标签的使用。
1.3 保举指数:★★

2 结果图:

3.代码:
#---导出模块---from tkinter import *from tkinter import filedialogfrom pygame.locals import *import pygameimport sysimport osimport time#---游戏初始化以及窗口的界说八、标猫先森收集资本站题八、巨细配置---pygame.init()
root = Tk()#窗口root.title("音乐播放器")#标题root.geometry("1000x800+400+200")#更改巨细以及位置#---gif的动画配置以及插入---numIdx = 6 # gif的帧数# 添补6帧内容到frames,注重111.gif路径frames = [PhotoImage(file='/home/xgj/Desktop/音乐播放器/111.gif', format='gif -index %i' %(i)) for i in range(numIdx)]# 准时器函数def update(idx):
frame = frames[idx]
idx += 1 # 下一帧的序号:在0,1,2,3,4,5之间轮回(共6帧)
label.configure(image=frame) # 表现当前帧的图片
root.after(100, update, idx%numIdx) # 0.1秒(100毫秒)以后继承实行准时器函数(update)label = Label(root)
label.pack()
root.after(0, update, 0) # 当即启动准时器函数(update)#---增添靠山图片---注重bj的路径配置photo = PhotoImage(file="/home/xgj/Desktop/音乐播放器/bj.png")
theLabel = Label(root,text="欢欣源泉,\n自界说开心",justify=LEFT,
image=photo,compound = CENTER,font=("汉文行楷",20),fg = "green")#小我喜好用place来结构位置theLabel.place(x=猫先森收集资本站10,y=500)#---界说按钮功能的函数--- def pause():
pygame.mixer.music.pause()def stop():
pygame.mixer.music.stop()def start():
pygame.mixer.music.unpause()def callback():
file = filedialog.askopenfilename() #调出路猫先森收集资本站径,打开音乐文件
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
#---按钮配置:位置八、名字八、下令功能等b = Button(root,text = "选择音乐",bg='yellow',co妹妹and = callback)
b.place(x=10,y=10)
f =Button(root,text = "停息",bg='yellow',co妹妹and = pause)
f.place(x=10,y=50)
bs =Button(root,text = "继承",bg='yellow',co妹妹and = start)
bs.place(x=10,y=90)
bst =Button(root,text = "住手",bg='yellow',co妹妹and = stop)
bst.place(x=10,y=130)
l = Label(root, text="迎接来到自界说音乐播放器!",bg='pink',fg='blue')
l.place(x=10,y=180)#---轮回启动---root.mainloop()4.所用到的gif以及bg图:


5.盼望喜好,分享出来,也能够在这底子上DIY,本身继承改良以及美化。
泉源:https://mxswl.net/zdmzt/74020.html |
|