[Python] python通过wxPython打开一个音频文件并播放的代码 →→→→→进入此内容的聊天室

来自 , 2019-11-06, 写在 Python, 查看 100 次.
URL http://www.code666.cn/view/161fd33f
  1. ''' wx_lib_filebrowsebutton_sound.py
  2. select a sound file and play it
  3. wx.lib.filebrowsebutton.FileBrowseButton(parent, labelText, fileMask)
  4. (combines wx.TextCtrl and wxFileDialog widgets)
  5. wx.Sound(fileName, isResource=False)
  6. tested with Python27 and wxPython291  by  vegaseat  25jul2013
  7. '''
  8. import wx
  9. import wx.lib.filebrowsebutton
  10. class MyFrame(wx.Frame):
  11.     def __init__(self, parent, mytitle, mysize):
  12.         wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle,
  13.             size=mysize)
  14.         self.SetBackgroundColour("green")
  15.         panel = wx.Panel(self)
  16.         # mask file browser to look for .wav sound files
  17.         self.fbb = wx.lib.filebrowsebutton.FileBrowseButton(panel,
  18.             labelText="Select a WAVE file:", fileMask="*.wav")
  19.         self.play_button = wx.Button(panel, wx.ID_ANY, ">> Play")
  20.         self.play_button.Bind(wx.EVT_BUTTON, self.onPlay)
  21.         # setup the layout with sizers
  22.         hsizer = wx.BoxSizer(wx.HORIZONTAL)
  23.         hsizer.Add(self.fbb, 1, wx.ALIGN_CENTER_VERTICAL)
  24.         hsizer.Add(self.play_button, 0, wx.ALIGN_CENTER_VERTICAL)
  25.         # create a border space
  26.         border = wx.BoxSizer(wx.VERTICAL)
  27.         border.Add(hsizer, 0, wx.EXPAND|wx.ALL, 10)
  28.         panel.SetSizer(border)
  29.     def onPlay(self, evt):
  30.         filename = self.fbb.GetValue()
  31.         self.sound = wx.Sound(filename)
  32.         # error handling ...
  33.         if self.sound.IsOk():
  34.             self.sound.Play(wx.SOUND_ASYNC)
  35.         else:
  36.             wx.MessageBox("Missing or invalid sound file", "Error")
  37. app = wx.App(0)
  38. # create a MyFrame instance and show the frame
  39. mytitle = "wx.lib.filebrowsebutton and wx.Sound"
  40. width = 600
  41. height = 90
  42. MyFrame(None, mytitle, (width, height)).Show()
  43. app.MainLoop()
  44. #//python/8468

回复 "python通过wxPython打开一个音频文件并播放的代码"

这儿你可以回复上面这条便签

captcha