Upgrade from 2.8.7.1 to 2.8.8.1 breaks code?

mercado mercado wrote:

I just upgraded from wxPython 2.8.7.1 <http://2.8.7.1/&gt; to 2.8.8.1 <http://2.8.8.1/&gt; on my machine (Ubuntu Hardy 8.04.1), and the following code stopped working:

(snippet from larger program)
------------------------------------------------------------------------------
self.text = wx.TextCtrl(self.panel, -1)
self.list = wx.ListCtrl(self.panel, -1, style=wx.LC_REPORT
                      > wx.BORDER_NONE
                      > wx.LC_EDIT_LABELS
                      )

self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyStroke)
self.list.Bind(wx.EVT_KEY_DOWN, self.OnKeyStroke)

def OnKeyStroke(self, event):
    id = event.EventObject.Id <http://event.eventobject.id/&gt;

    if id == self.text.Id <http://self.text.id/&gt;:
        (snip #1)
    elif id == self.list.Id <http://self.list.id/&gt;:
        (snip #2)
------------------------------------------------------------------------------

If I print out self.text.Id <http://self.text.id/&gt; and self.list.Id , they give me values of -203 and -204, respectively.

When an event fires from the text control, id has a value of -203, and everything works as it did before. However, when an event fires from the list control, id has a value of -205, the compare fails, and none of the code in (snip #2) runs. This same code worked fine with wxPython 2.8.7.1.

Am I doing something wrong by using event.EventObject.Id to identify the control that fired an event, or is this a bug in wxPython?

I don't see anything in the svn log that sounds like a change related to this so I'm not sure yet what it was before, but I can tell you why it is behaving the way it is now. The generic listctrl used on GTK and Mac is actually implemented with sub-windows, and those sub-windows have their own IDs. Since the main sub-window has the focus when the key event happens then that is which ID is put into the key event. Try comparing with self.list.MainWindow.Id instead of self.list.Id, or see Chris' reply for a better way to do it.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

For example, if the user presses F5, then I want to refresh the screen no matter if the focus is on the TextCtrl or the ListCtrl.

This sounds like what you want is an AcceleratorTable, rather than catching the EVT_KEY_DOWN

Ahh… I didn’t know that this existed. I think this is exactly what I want.

http://www.wxpython.org/docs/api/wx.AcceleratorTable-class.html

Thanks again Christopher.

mercado mercado wrote:

2. Using self.list.MainWindow.Id <http://self.list.MainWindow.Id> instead of self.list.Id <http://self.list.Id> *does* work. I am currently developing using GTK, so that behaviour makes sense in light of Robin's explanation, however I would like this code to work on all platforms so I need to find something else eventually.

For the record it would probably work on Windows too since GetMainWindow returns itself on that platform. On Mac however it may not since there is an additional extra layer there to allow selecting either the native listctrl or the generic one.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!