The way Python opens an audio file and plays it through Wxpython _python

Source: Internet
Author: User
Tags error handling

This example describes how Python opens an audio file through Wxpython and plays it. Share to everyone for your reference. Specifically as follows:

This snippet uses the Wx.lib.filebrowsebutton.FileBrowseButton control to open a WAV file and uses Wx.sound to play

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 (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, 0, WX. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.