from pygame import mixer#从PYGAME库导入MIXEWR包用来放声音
mixer.init()#假惺惺初始化一下
sound=mixer.Sound("qq.wav")#打开音乐文件句柄
sound.play()#播放
def menu(status):#菜单显示
os.system("cls")#控制台清屏
print("wav player {}".format(status))
print("---")
print("1.play")
print("2.the next one")
print("3.the last one")
print("4.stop")
print("0.over")
print("---")
def playwav(song):#播放音乐,注释:比下一个函数就少个了停止
global status,sound
sound=mixer.Sound(wavfiles[index])
sound.play(loops=0)
status="{} is playing".format(wavfiles[index])
def playNewwav(song):#播放新音乐
global status,sound#全局变量
sound.stop()#stop the current song
sound=mixer.Sound(wavfiles[index])#get the new song
sound.play(loops=0)#play the new song,loops=0不循环
status="{} is playing".format(wavfiles[index])#更新信息口串
from pygame import mixer#导入混合库(音乐)
import glob,os#导入GLOB,系统模块
'''
glob模块是最简单的模块之一内容非常少
用它可以查找符合特定规则的文件路径名
跟使用windows下的文件搜索差不多
查找文件只用到三个匹配符:
”*”匹配0个或多个字符
”?”匹配单个字符
”[]”匹配指定范围内的字符
如:[0-9]匹配数字。
'''
mixer.init()#假惺惺初始化
wavfiles=glob.glob("*.wav")#get the list
index=0#下标初始化为0
status=""#信息串为0
sound=mixer.Sound(wavfiles[index])#get no.index sound
while True:#死循环
menu(status)#传入信息串
choice=int(input("choose : "))#读出选择
if choice==1:
playwav(wavfiles[index])#播音乐
elif choice==2:
index+=1#下标加一
if index==len(wavfiles):#如果超限
index=0#就归0
playNewwav(wavfiles[index])#播放
elif choice==3:
index-=1#下标减一
if index<0:#如果小于0
index=len(wavfiles)-1#就变最大下标
playNewwav(wavfiles[index])#播放
elif choice==4:
sound.stop()#停止播放
status="stop"#更新状态
else:#否则
break#跳出
sound.stop()#关掉音乐
print("over")#输出,OVER
#音乐播放器
#本例中使用的两个StringVar类型变量是关键
def choose():
global playsong#引用全局变量
msg.set("\n playing song: "+choice.get())#改变信息,获得choice这个StringVar的文本信息,注意是文本信息,不是其变量值
playsong=choice.get()#注意!就是在这里更改所选歌目
def pausemp3():
global playsong#引用全局变量
mixer.music.pause()
msg.set("\n pause song {}".format(playsong))
def increase():
global volume#引用全局变量
volume+=0.1
if volume>=1:
volume=1
mixer.music.set_volume(volume)#设置音量大小
def decrease():
global volume#引用全局变量
volume-=0.1
if volume<=0.3:
volume=0.3
mixer.music.set_volume(volume)#设置音量大小
def playmp3():
global status,playsong,preplaysong#引用全局变量
choose()#得到当前选择的歌
if playsong==preplaysong:#同一首歌
if not mixer.music.get_busy():#如果没有正忙
mixer.music.load(playsong)#加载歌曲
mixer.music.play(loops=-1)#循环播放
else:#已在忙
mixer.music.unpause()#可以继续播了
msg.set("\n playing song: {}".format(playsong))#更新MSG
else:#不同歌
playNewmp3()#播放
preplaysong=playsong#更新旧歌
def playNewmp3():
global playsong#引用全局变量
mixer.music.stop()#停止旧音乐
mixer.music.load(playsong)#加载新音乐
mixer.music.play(loops=-1)#循环
msg.set("\n playing song: {}".format(playsong))#更新MSG信息
def stopmp3():
mixer.music.stop()#停止音乐
msg.set("\n stop")#输出提示信息
def exitmp3():
mixer.music.stop()#停止音乐
win.destroy()#破坏窗口
import tkinter as tk #导入界面库
from pygame import mixer #从PYGAME导入混合模块
import glob #查找模块
mixer.init() #假惺惺初始化
win=tk.Tk() #申请窗口
win.geometry("640x380") #设计窗口大小
win.title("mp3 player") #窗口标题
labeltitle=tk.Label(win,text="\nmp3 player",fg="red",font=("新细明体",12))#初始化标签
labeltitle.pack() #打包
frame1=tk.Frame(win) #定义框架
frame1.pack() #打包
mp3files=glob.glob("*.mp3") #查后MP3后缀的文件
playsong=preplaysong="" #初始化变量
index=0 #初始值
volume=0.6 #初始值
choice=tk.StringVar() #得到窗口的字符串变量
print(choice) #输出当前TK的内部字符串变量
for mp3 in mp3files: #遍历确定单选按钮
rbtem=tk.Radiobutton(frame1,text=mp3,variable=choice,value=mp3,command=choose)#创建按钮
#注意上面这句用了choice这个StringVar类对象来作为变量值
if(index==0): #下标为0
rbtem.select() #选择
playsong=preplaysong=mp3 #播放
rbtem.grid(row=index,column=0,sticky='w')#方格中的内容INDEX行只有第0列,放贴着左边
index+=1 #下标加一
msg=tk.StringVar()#在获得符串变量,注意这其实就相当于申请了一个字符串,仅此而已
print(msg)#输出MSG这个变量的值,这个值是指TK内部给这个变量的命名,不是他显示的文本
msg.set("\nplay the music: ")#改变当前这个上字符串
label=tk.Label(win,textvariable=msg,fg="blue",font=("新细明体",10))#设置标签,其文本就是MSG这个StringVar类对象
label.pack()#打包
labelsep=tk.Label(win,text="\n")#设置标签
labelsep.pack()#打包
frame2=tk.Frame(win)#在win窗口下继续申请框架
frame2.pack()#打包
button1=tk.Button(frame2,text="play",width=8,command=playmp3)#定义按钮,父对像框架2,文本play,宽8像素,点击命令
button1.grid(row=0,column=0,padx=5,pady=5)#第0行第0列,X方向左右各延伸5像素,Y方向上下各延伸5像素
button2=tk.Button(frame2,text="pause",width=8,command=pausemp3)#定义按钮,父对像框架2,文本pause,宽8像素,点击命令
button2.grid(row=0,column=1,padx=5,pady=5)#第0行第0列,X方向左右各延伸5像素,Y方向上下各延伸5像素
button3=tk.Button(frame2,text="increase",width=8,command=increase)#定义按钮,父对像框架2,文本increase,宽8像素,点击命令
button3.grid(row=0,column=2,padx=5,pady=5)#第0行第0列,X方向左右各延伸5像素,Y方向上下各延伸5像素
button4=tk.Button(frame2,text="decrease",width=8,command=decrease)#定义按钮,父对像框架2,文本decrease,宽8像素,点击命令
button4.grid(row=0,column=3,padx=5,pady=5)#第0行第0列,X方向左右各延伸5像素,Y方向上下各延伸5像素
button5=tk.Button(frame2,text="stop",width=8,command=stopmp3)#定义按钮,父对像框架2,文本stop,宽8像素,点击命令
button5.grid(row=0,column=4,padx=5,pady=5)#第0行第0列,X方向左右各延伸5像素,Y方向上下各延伸5像素
button6=tk.Button(frame2,text="exit",width=8,command=exitmp3)#定义按钮,父对像框架2,文本exit,宽8像素,点击命令
button6.grid(row=0,column=5,padx=5,pady=5)#第0行第0列,X方向左右各延伸5像素,Y方向上下各延伸5像素
win.protocol("WW_DELETE_WINDOW",exitmp3)#当点击了关闭窗口按钮时执行exitmp3函数
win.mainloop()#等待窗口消息
'''
小结一下:本例中用了两个StringVar变量
StringVar是TK下自设的一个变量,特点是每调用一次都会自加1产生一个新值,在控制台可以看到是PY_VARX的形式,X是序号
choice是其中一个StringVar变量,上面赋给单选按钮的variable成员变量=choice时每个按钮都不一样
msg是其中一个StringVar变量,被用作标签的textvariable文本,然后每次都SET方法设置其显示文本
注意是设置其显示文本,就是说这个MSG变量的值是没变的,也是PY_VARX,但他也有显示文本的成员属性
其余用处其实与一般的STRING差不多,起码本例是这样,抛砖引玉吧
'''