结合网上的一些代码写下自己的理解,
# _*_ coding:utf8
import Tkinter
import tkMessageBox
from win32com.client import Dispatch
import tkFileDialog
from Tkinter import *
import sys
from sys import exit
class music(object):
def init(self,root):
self.root=root
self.create_menu(self.root)
fm = Frame(root,height=500,bg='#ffffff', width=800)
Label(root, text="播放器",width=100,bg='#ffffff', font=("Arial",10,)).pack()
frmL = Frame(fm)
#列表框
var = StringVar
self.listbox = Listbox(frmL,listvariable=var,width=65,height=100,font=("Arial",10))
self.listbox.pack()
frmR=Frame(fm,bg='#ffffff')
#lis = Listbox(frmR,listvariable=var,width=45,height=70,font=("Arial",12)).pack(side=TOP)
#button
previous_play = Tkinter.Button(frmR,text='上一首',bg='#cccccc',width=8,height=1,command=self.previous_p)
previous_play.grid(row=1,column=1,padx=10, pady=35)
play = Tkinter.Button(frmR,text='播放/暂停',bg='#cccccc',width=8,height=1,command=self.play_pause)
play.grid(row=1,column=2, padx=10, pady=5)
next_play = Tkinter.Button(frmR,text='下一首',bg='#cccccc',width=8,height=1,command=self.next_p)
next_play.grid(row=1,column=3,padx=10, pady=5)
stop_play = Tkinter.Button(frmR,text='停止',bg='#cccccc',width=8,height=1,command=self.stop_p)
stop_play.grid(row=1,column=4,padx=10, pady=5)
self.volum = Tkinter.IntVar()
scale = Tkinter.Scale(frmR,variable = self.volum, bg='#ffffff',orient = Tkinter.HORIZONTAL, length = 315,command=self.vol)
scale.set(50)
scale.grid(row=2,column=0,columnspan=5)
frmL.pack(side=LEFT)
frmR.pack(side=RIGHT)
fm.pack()
self.mu = Dispatch('WMPlayer.OCX')
self.playing=False
self.root.mainloop()
#添加菜单
def create_menu(self,root):
self.root = root
menubar = Tkinter.Menu(self.root)
filemenu = Tkinter.Menu(menubar,tearoff=0)#tearoff=0菜单不能独立出来
filemenu.add_command(label='打开音乐文件',command=self.open_files,accelerator='Ctrl+O')
filemenu.add_command(label='退出',command=self.close,accelerator='Ctrl+X')
root.bind_all("<Control-o>", lambda event:self.hello())
root.bind_all("<Control-x>", lambda event:self.close())
abort = Tkinter.Menu(menubar,tearoff=0)
abort.add_command(label='关于播放器',command=self.tkabort)
menubar.add_cascade(label='文件',menu=filemenu,)
menubar.add_cascade(label='关于',menu=abort)
root.config(menu=menubar)
def vol(self, none):
self.mu.settings.Volume = int(self.volum.get())
def previous_p(self):
self.mu.controls.previous()
def play_pause(self):
if self.playing==False:
self.mu.controls.play()
self.playing=True
elif self.playing==True:
self.mu.controls.pause()
self.playing=False
def stop_p(self):
self.mu.controls.stop()
def next_p(self):
self.mu.controls.next()
def open_files(self):
filenames = tkFileDialog.askopenfilenames(initialdir = './',title='选择音乐',filetypes=[('MP3','*.mp3'),('FlAC','*.flac'),("WMA","*.wma"),("WAV","*.wav")])
# listboxs = self.contents(self.root)
for i in filenames:
if i:
media = self.mu.newMedia(i)
self.mu.currentPlaylist.appendItem(media)
l=i.split('/')
self.listbox.insert(END,l[-1])
print type(self.mu.currentPlaylist)
return filenames
#abort
def tkabort(self):
tkMessageBox.showinfo('关于','播放器1.0')
#关闭程序
def close(self):
exit()
if __name__=='__main__':
root =Tkinter.Tk()
root.geometry('800x500+600+100')
root.resizable(width=False, height=True)
root.title('音乐')
root.iconbitmap('error')
m = music()
m.init(root)
重点内容****1. from win32com.client import Dispatch
遗憾的是在网上并没有找到多少关于Dispatch这个方法的知识点,似乎可以理解为它是一个连接软件与文件的工具的方法,就是可以操作一些文件(原谅我的菜),
对于MP3文件:
# Python supports COM, if you have the Win32 extensions
# check your Python folder eg. D:\Python23\Lib\site-packages\win32com
# also http://starship.python.net/crew/mhammond/win32/Downloads.html
# this program will play MP3, WMA, MID, WAV files via the WindowsMediaPlayer
from win32com.client import Dispatch
mp = Dispatch("WMPlayer.OCX")
# use an mp3 file you have ...
#tune = mp.newMedia("C:/Program Files/Common Files/HP/Memories Disc/2.0/audio/Swing.mp3")
# or copy one to the working folder ...
#tune = mp.newMedia("Bier1.mp3")
# you can also play wma files, this cool sound came with XP ...
tune = mp.newMedia("C:/WINDOWS/system32/oobe/images/title.wma")
mp.currentPlaylist.appendItem(tune)
mp.controls.play()
# to stop playing use
raw_input("Press Enter to stop playing")
mp.controls.stop()
对于一些其他的文件:
w = win32com.client.Dispatch("PythonDemos.Utilities")
w = win32com.client.Dispatch("ctypes.SumObject")
w=win32com.client.Dispatch("InternetExplorer.Application")
w = win32com.client.Dispatch("Excel.Application")
另外其他内容:https://wenku.baidu.com/view/b129df4c79563c1ec5da71dd.html?from=search
**2.**Python中的打开文件对话框
(1)
from Tkinter import *
from FileDialog import *
root = Tk()
fo = LoadFileDialog(root) # 创建打开文件对话框
filename = fo.go() # 显示打开文件对话框,并获取选择的文件名称
print filename
root.mainloop()
但是界面效果比较简陋(不是本地风格),而且遇到中文名称也会显示为乱码。所以FileDialog.LoadFileDialog用于快速原型验证倒是不错,要拼凑一个稍微上得了台面的界面还是差了点.
(2)
import win32ui
dlg = win32ui.CreateFileDialog(1) # 1表示打开文件对话框
dlg.SetOFNInitialDir('E:/Python') # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName() # 获取选择的文件名称
print filename
这个打开文件对话框的界面比较友好,是Windows本地风格的,中文显示也正常,但缺点是只能在Windows上有效.
(3)
import tkFileDialog
filename = tkFileDialog.askopenfilename(initialdir = ‘E:/Python’)
print filename
界面也是友好的本地风格,与使用win32ui.CreateFileDialog类似,但是优点是tkFileDialog是跨平台的
相关链接:
<http://blog.sina.com.cn/s/blog_76129c6f0101892q.html/>
<http://www.jb51.net/article/67001.htm/>
<http://blog.sina.com.cn/s/blog_4b5039210100ezzq.html/>