Problem

I want to generate an wxEVT_COMMAND_LISTBOX_SELECT event by hand.

Context

I am using wxPython on Debian Gnu/Linux (unstable). The relevant packages are:

The documentation that I refer to below is part of the libwxgtk2.5-python package and lives in my computer on /usr/share/doc/wxwin2.5-doc/wxWindows-manual.html/.

My path to the solution (and some problems left behind)

My documentation does not say that wxListBox inherits from wxControl, but it does (or it appears to). This appears to be a documentation bug.

So, the method wxListBox.Command exists. The docs on wxControl say that wxControl::wxCommand is like

void Command(wxCommandEvent& event)

The next problem is to generate an wxCommandEvent instance. The docs say that the constructor of wxCommandEvent is like

wxCommandEvent(WXTYPE commandEventType = 0, int id = 0)

To retrieve the EventType for an event type ;) you can do :

yourPyEventBinder.evtType[0]

The solution

All of the above put together, the line that produces an EVT_LISTBOX event is:

    self.Command(wx.wxCommandEvent(wx.EVT_LISTBOX.evtType[0], self.GetId()))

where self is the wxListBox instance.

To do

Example

This example demonstrates a simple window with a button, combo box, and text field. Pushing the button creates a CommandEvent and tells the combo box to process it. The event contains client data, as well.

import wx

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        btn = wx.Button(self, label="create event")
        btn.Bind(wx.EVT_BUTTON, self.OnButton)
        
        self.CB = wx.ComboBox(self, style=wx.CB_READONLY)
        self.CB.Append('one', '1')
        self.CB.Append('two', '2')
        self.CB.Bind(wx.EVT_COMBOBOX, self.OnCombo)
        
        self.text=  wx.TextCtrl(self, style=wx.TE_READONLY)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        sizer.Add(self.CB, 0, wx.LEFT | wx.RIGHT , 10)
        sizer.Add(self.text, 0, wx.ALL, 10)
        self.SetSizerAndFit(sizer)
    
    def OnButton(self, event):
        e = wx.CommandEvent(wx.EVT_COMBOBOX.evtType[0], self.CB.GetId() )
        e.SetString('three')
        e.SetClientData('3')
        self.CB.Command(e)
    
    def OnCombo(self,event):
        self.text.SetValue("%s %s" % (event.GetString(), event.GetClientData()))
        
        
class TestFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.NB = TestPanel(self)
        sizer.Add(self.NB, 1, wx.EXPAND | wx.GROW)
        self.SetSizerAndFit(sizer)
        self.Layout()

from wx.lib.mixins.inspection import InspectionMixin
class App(wx.App, InspectionMixin):
    def OnInit(self):
        self.Init()
 
        frame = TestFrame(None)
        self.SetTopWindow(frame)
        frame.Show(True)
        return True


app = App(0)
app.MainLoop()
app.Destroy()

The only problem with this code is the generated event won't change the value in the combo box.

EventGeneration (last edited 2009-08-01 19:53:39 by 65)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.