This article describes how Python opens and plays a WAV file using Wxpython. Share to everyone for your reference. The implementation method is as follows:
"' wx_lib_filebrowsebutton_sound.py Select a sound file and play it Wx.lib.filebrowsebutton.FileBrowseButton (parent, LA Beltext, 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.f Ilebrowsebutton 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 (P Anel, 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, 0Wx. align_center_vertical) # Create a border space border = wx. Boxsizer (WX. VERTICAL) border. ADD (Hsizer, 0, WX. Expand|wx. All) 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 = height = myframe (None, MyTitle, (width, height))." Show () app. Mainloop ()
I hope this article will help you with your Python programming.