|
楼主 |
发表于 2017-3-10 22:09:53
|
显示全部楼层
- from tkinter import *
- from tkinter import filedialog
- import pygame
- pygame.mixer.init()
- pygame.mixer.music.set_volume(0.2)
- signal_1 = 0
- volume = 0.1
- NAME_1 = []
- NAME_2 = []
- INDEX = 0
- NAME = ''
- def get(event):
- button2['state'] = ACTIVE
- button4['state'] = DISABLED
- global NAME
- global INDEX
- try:
- NAME = lib.get(lib.curselection())
- INDEX = lib.curselection()[0]
- except:
- pass
- def play():
- global signal_1
- global NAME
- signal_1 =signal_1
- if signal_1 ==1:
- pygame.mixer.music.unpause()
- signal_1 = 0
- button2['state'] = DISABLED
- button3['state'] = ACTIVE
- button4['state'] = ACTIVE
-
- else:
- pygame.mixer.music.load(NAME)
- pygame.mixer.music.play()
- print(lib.get(INDEX+1))
- pygame.mixer.music.queue(lib.get(INDEX+1))
- button2['state'] = DISABLED
- button3['state'] = ACTIVE
- button4['state']= ACTIVE
- def pause():
- pygame.mixer.music.pause()
- global siganl_1
- signal_1=1
- button2['state'] = ACTIVE
-
- button4['state'] = DISABLED
- def nextsong():
- global INDEX
- global NAME
- INDEX = INDEX+1
- NAME = lib.get(INDEX)
- play()
- def lastsong():
- global INDEX
- global NAME
- INDEX = INDEX-1
- NAME = lib.get(INDEX)
- play()
- def playback():
- global signal_1
- signal_1 = 0
- pygame.mixer.music.rewind()
- def volumeup():
- global volume
- if volume >=1.0:
- button6['state'] = DISABLED
- button7['state'] = ACTIVE
- else:
- volume = volume+0.05
- button6['state'] = ACTIVE
- button7['state'] = ACTIVE
- pygame.mixer.music.set_volume(volume)
- def volumedown():
- global volume
- if volume <=0:
- button7['state'] = DISABLED
- button6['state'] = ACTIVE
- else:
- volume = volume-0.05
- button6['state'] = ACTIVE
- button7['state'] = ACTIVE
- pygame.mixer.music.set_volume(volume)
- def openfile():
- global NAME_1
- global NAME_2
- NAME_1 = filedialog.askopenfilenames(filetypes = [('MP3','.mp3')])
- for name in NAME_1:
- if name not in NAME_2:
- NAME_2.append(name)
- for item in NAME_2:
- lib.insert(END,item)
- lib.grid(row = 1,columnspan = 7,sticky = W)
- sb.config(command = lib.yview)
- def savefile():
- filedialog.asksaveasfilename()
-
- root = Tk()
- root.title('星云播放器')
- root.resizable(False, False)
- sb = Scrollbar(root)
- sb.grid(row = 1,column = 7,sticky = W,ipady = 80)
- lib = Listbox(root,height = 12,width = 59,yscrollcommand = sb.set)
- lib.bind("<ButtonRelease-1>",get)
- lib.grid(row = 1,columnspan = 7,sticky = W)
- sb.config(command = lib.yview)
- menubar = Menu(root)
- filename = Menu(menubar,tearoff = False)
- filename.add_command(label = '打开',command = openfile)
- filename.add_command(label = '保存',command = savefile)
- menubar.add_cascade(label = '文件',menu = filename)
- root.config(menu = menubar)
- button1 = Button(root,text = "上一首",width = 7,height = 1,command = lastsong)
- button1.grid(row = 2,column = 0,sticky = W)
- button2 = Button(root,text = '播放',width = 7,height = 1,command = play,state = ACTIVE)
- button2.grid(row = 2,column=1,sticky = W)
- button3 = Button(root,text = '重新播放',width = 7,height = 1,command = playback,state = DISABLED)
- button3.grid(row = 2,column=2,sticky = W)
- button4 = Button(root,text = '暂停',width = 7,height = 1,command = pause,state = DISABLED)
- button4.grid(row = 2,column=3,sticky = W)
- button5 = Button(root,text = "下一首",width = 7,height = 1,command = nextsong)
- button5.grid(row = 2,column=4,sticky = W)
- button6 = Button(root,text = "音量+",width = 7,height = 1,command = volumeup)
- button6.grid(row = 2,column=5,sticky = W)
- button7 = Button(root,text = "音量-",width = 7,height = 1,command = volumedown)
- button7.grid(row = 2,column=6,sticky = W)
- mainloop()
复制代码 |
|