python使用wxpython打开并播放wav文件的方法

本文实例讲述了python使用wxpython打开并播放wav文件的方法。分享给大家供大家参考。具体实现方法如下:

”’ wx_lib_filebrowsebutton_sound.py
select a sound file and play it
wx.lib.filebrowsebutton.filebrowsebutton(parent, labeltext, filemask)
(combines wx.textctrl and wxfiledialog widgets)
wx.sound(filename, isresource=false)
tested with python27 and wxpython291 by vegaseat 25jul2013
”’
import wx
import wx.lib.filebrowsebutton
class myframe(wx.frame):
def __init__(self, parent, mytitle, mysize):
wx.frame.__init__(self, parent, wx.id_any, mytitle,
size=mysize)
self.setbackgroundcolour(“green”)
panel = wx.panel(self)
# mask file browser to look for .wav sound files
self.fbb = wx.lib.filebrowsebutton.filebrowsebutton(panel,
labeltext=”select a wave file:”, filemask=”*.wav”)
self.play_button = wx.button(panel, wx.id_any, “>> play”)
self.play_button.bind(wx.evt_button, self.onplay)
# setup the layout with sizers
hsizer = wx.boxsizer(wx.horizontal)
hsizer.add(self.fbb, 1, wx.align_center_vertical)
hsizer.add(self.play_button, 0, wx.align_center_vertical)
# create a border space
border = wx.boxsizer(wx.vertical)
border.add(hsizer, 0, wx.expand|wx.all, 10)
panel.setsizer(border)
def onplay(self, evt):
filename = self.fbb.getvalue()
self.sound = wx.sound(filename)
# error handling …
if self.sound.isok():
self.sound.play(wx.sound_async)
else:
wx.messagebox(“missing or invalid sound file”, “error”)
app = wx.app(0)
# create a myframe instance and show the frame
mytitle = “wx.lib.filebrowsebutton and wx.sound”
width = 600
height = 90
myframe(none, mytitle, (width, height)).show()
app.mainloop()

希望本文所述对大家的python程序设计有所帮助。

发表评论