一、安装模块
- 学习使用pygame模块,实现模块相应操作。
- 安装相应模块。
在windows运行窗口输入cmd后回车(或者在pycharm的终端)输入:pip install pygame进行安装即可使用。
二、编写代码(代码已测试通过) import pygame import tkinter as tkr from tkinter.filedialog import askdirectory import os music_player = tkr.Tk() music_player.title("我的音乐播放器") #播放器标题 music_player.geometry("220x650") #播放器大小(宽*高) directory = askdirectory() os.chdir(directory) song_list = os.listdir() play_list = tkr.Listbox(music_player, font="Helvetica 12 bold", bg='green', selectmode=tkr.SINGLE) #歌单字体,底色,选择模式 for item in song_list: pos = 0 play_list.insert(pos, item) pos += 1 pygame.init() pygame.mixer.init() def play(): """播放""" pygame.mixer.music.load(play_list.get(tkr.ACTIVE)) var.set(play_list.get(tkr.ACTIVE)) pygame.mixer.music.play() def stop(): """停止""" pygame.mixer.music.stop() def pause(): """暂停""" pygame.mixer.music.pause() def unpause(): """取消暂停""" pygame.mixer.music.unpause() #播放器菜单 Button1 = tkr.Button(music_player, width=3, height=1, font="Helvetica 12 bold", text="播放", command=play, bg="blue", fg="white") Button2 = tkr.Button(music_player, width=3, height=1, font="Helvetica 12 bold", text="停止", command=stop, bg="red", fg="white") Button3 = tkr.Button(music_player, width=3, height=1, font="Helvetica 12 bold", text="暂停", command=pause, bg="purple", fg="white") Button4 = tkr.Button(music_player, width=3, height=1, font="Helvetica 12 bold", text="取消暂停", command=unpause, bg="orange", fg="white") Button5 = tkr.Button(music_player, width=3, height=1, font="Helvetica 12 bold", text="退出", command=exit, bg="blue", fg="white") var = tkr.StringVar() song_title = tkr.Label(music_player, font="Helvetica 12 bold", textvariable=var) song_title.pack() #菜单显示位置 Button1.pack(fill="x") Button2.pack(fill="x") Button3.pack(fill="x") Button4.pack(fill="x") Button5.pack(fill="x") play_list.pack(fill="both", expand="yes") music_player.mainloop()
以上代码已测试通过,如有问题请加qq群(808738421)申请提供技术支持。