2016-08-20 33 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__'類似),但會將成員私人/不可見於課程外部。我已經更新了這個例子。 –