2016-08-20 34 views
0

我使用wxPython软件包制作了一个快速且脏的音板,并且想知道如何通过实现要播放的声音的滚动列表来实现。wxPython:使用面板创建共鸣板

这里是什么,我想传达一个画面: http://i.imgur.com/av0E5jC.png

这里是我到目前为止的代码:

import wx 

class windowClass(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     super(windowClass,self).__init__(*args,**kwargs) 
     self.basicGUI() 
    def basicGUI(self): 
     panel = wx.Panel(self) 
     menuBar = wx.MenuBar() 
     fileButton = wx.Menu() 
     editButton = wx.Menu() 
     exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...') 

     menuBar.Append(fileButton, 'File') 
     menuBar.Append(editButton, 'Edit') 

     self.SetMenuBar(menuBar) 
     self.Bind(wx.EVT_MENU, self.Quit, exitItem) 

     wx.TextCtrl(panel,pos=(10,10), size=(250,150)) 

     self.SetTitle("Soundboard") 
     self.Show(True) 
    def Quit(self, e): 
     self.Close() 
def main(): 
    app = wx.App() 
    windowClass(None) 
    app.MainLoop() 


main() 

我的问题是,一个人如何在加载声音列表该面板并点击某个按钮来播放该声音。我并不在乎实现暂停和快进功能,因为这只会播放真正快速的声音文件。

在此先感谢。

回答

0

刚刚删除了文本小部件,替换为一个列表框,并在项目点击上挂钩了一个回调,稍微详细一点:点击时,它找到该项目的位置,检索标签名称并获取字典中的文件名 (我们希望播放带有路径的.wav文件,但不一定显示完整的文件名)

我重构了代码,所以回调和其他属性是私人的,有助于可读性。

import wx 

class windowClass(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     super(windowClass,self).__init__(*args,**kwargs) 
     self.__basicGUI() 
    def __basicGUI(self): 
     panel = wx.Panel(self) 
     menuBar = wx.MenuBar() 
     fileButton = wx.Menu() 
     editButton = wx.Menu() 
     exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...') 

     menuBar.Append(fileButton, 'File') 
     menuBar.Append(editButton, 'Edit') 

     self.SetMenuBar(menuBar) 
     self.Bind(wx.EVT_MENU, self.__quit, exitItem) 

     self.__sound_dict = { "a" : "a.wav","b" : "b.wav","c" : "c2.wav"} 
     self.__sound_list = sorted(self.__sound_dict.keys()) 

     self.__list = wx.ListBox(panel,pos=(20,20), size=(250,150)) 
     for i in self.__sound_list: self.__list.Append(i) 
     self.__list.Bind(wx.EVT_LISTBOX,self.__on_click) 

     #wx.TextCtrl(panel,pos=(10,10), size=(250,150)) 

     self.SetTitle("Soundboard") 
     self.Show(True) 

    def __on_click(self,event): 
     event.Skip() 
     name = self.__sound_list[self.__list.GetSelection()] 
     filename = self.__sound_dict[name] 
     print("now playing %s" % filename) 

    def __quit(self, e): 
     self.Close() 
def main(): 
    app = wx.App() 
    windowClass(None) 
    app.MainLoop() 

main() 
+0

法布尔,你是如何调用诸如__list和sound_list等内建函数,或者他们只是他们键入某些变量的方式? – kommander0000

+0

双下划线前缀与内置插件无关(后跟下划线也会与'__init__'类似),但会将成员私人/不可见于课程外部。我已经更新了这个例子。 –