wxPython:创建带有面板的声音板

2024-06-16 12:47:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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()

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

提前谢谢。在


Tags: self声音列表initdefargskwargsmenu
1条回答
网友
1楼 · 发布于 2024-06-16 12:47:33

只是删除了文本小部件,替换为一个列表框,并在item click上挂起一个回调函数,稍微详细说明一下:当单击时,它会找到项目的位置,检索标签名称并从字典中获取文件名 我们不需要显示完整的文件名,但不需要显示完整的文件名)

我重构了代码,使回调和其他属性是私有的,有助于实现。在

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

相关问题 更多 >