wxPython:使用面板创建音板

时间:2016-08-20 17:50:50

标签: python 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()

我的问题仍然是,如何加载该面板上的声音列表并单击某个按钮来播放该声音。我并不真正关心实现暂停和快进功能,因为这只会播放非常快速的声音文件。

提前致谢。

1 个答案:

答案 0 :(得分: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()