如何访问 wx.EVT_LISTBOX 事件的特定参数?

How do I access specific parameters of a wx.EVT_LISTBOX event?

我正在尝试设计一个 GUI,其中一个组件是具有多项选择功能的 wx.ListBox(样式 = wx.LB_MULTIPLE。)我还有另一个面板,我想在其中设置文本以匹配在列表框中选择的最后一项的详细说明。

我知道我可以用这种方式将 ListBox 绑定到一个函数: listbox_obj.Bind(wx.EVT_LISTBOX, self.set_description)

但是,当我定义方法时...

def set_description(self, event):

...我如何从 事件 参数推断列表框中的哪个项目是最后选择的项目,以及该项目是被选择还是取消选择?

EVT_LISTBOX 生成 wx.CommandEvent。 (参见 https://wxpython.org/Phoenix/docs/html/wx.CommandEvent.html

列表框事件有:IsSelection 和 GetSelection。

我认为您将不得不自己跟踪数据。
例如:

import wx

class TestListBox(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.OldSelections= []
        self.NewSelections = []
        self.aList = ['zero', 'one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
                      'twelve', 'thirteen', 'fourteen']
        self.lb = wx.ListBox(self, wx.NewId(), choices=self.aList, style=wx.LB_EXTENDED)
        self.lb.Bind(wx.EVT_LISTBOX, self.EvtMultiListBox)
        self.lb.SetSelection(0)
        self.Show()

    def EvtMultiListBox(self, event):
        self.NewSelections = self.lb.GetSelections()
        print('EvtMultiListBox: %s\n' % str(self.lb.GetSelections()))
        for i in self.NewSelections:
            if i not in self.OldSelections:
                print (self.aList[i],"was added")
        for i in self.OldSelections:
            if i not in self.NewSelections:
                print (self.aList[i],"was removed")
        self.OldSelections = self.NewSelections
        print("\n")

if __name__ == '__main__':
    app = wx.App()
    TestListBox(None)
    app.MainLoop()