Search code examples
pythonwxpython

wxPython : Creating a soundboard with panels


I am making a quick and dirty soundboard using the wxPython package and was wondering how to go by implementing a scroll-list of sounds to play.

Here is a picture of what I am trying to convey: https://i.sstatic.net/gZsGS.png

and here is my code so far:

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()

My question remains, how does one load a list of sounds on that panel and click a certain button to play that sound. I don't really care about implementing pause and fast forward features since this is only going to play really quick sound files.

Thanks in advance.


Solution

  • Just removed the text widget, replaced by a listbox, and hooked a callback on item click, slightly elaborate: when clicking, it finds the position of the item, retrieves the label name and fetches the filename in the dictionary (we want to play a .wav file with a path, but not necessarily display the full filename)

    I refactored the code so callbacks and other attributes are private, helps the lisibility.

    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()