Event Handling Anomaly

Mark Erbaugh mark at microenh.com
Mon Dec 3 08:30:06 PST 2007


I've run into an anomaly that can be demonstrated with this code:

################################################################

import wx

class Frame(wx.Frame):

    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        # reverse the following to lines to fix
        self.st = wx.StaticText(self, -1, size=(40,-1))
        self.lb = wx.ListBox(self, -1, choices=['one','two','three'])
        
        sz = wx.BoxSizer(wx.HORIZONTAL)
        sz.Add(self.lb, 0)
        sz.Add(self.st, 0)
        self.SetSizer(sz)
        self.Fit()

        # self.Bind(wx.EVT_LISTBOX, self.onLB, self.lb) # okay on exit
        self.lb.Bind(wx.EVT_LISTBOX, self.onLB)         # error on exit

    def onLB(self, event):
        self.st.SetLabel(self.lb.GetStringSelection())


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    Frame(None, -1, "").Show()
    app.MainLoop()

################################################################

When you run this example, if you select an item in the wx.ListBox, you
will get an error when you exit the app:

wx._core.PyDeadObjectError: The C++ part of the StaticText object has
been deleted, attribute access no longer allowed.

You don't get the error if you just launch the app and exit without
selecting an item in the wx.ListBox

You can eliminate the error by switching the order of the two lines
creating the wx.StaticText and the wx.ListBox.

You can also eliminate the error by binding the event handler to the
wx.Frame instead of to the wx.ListBox.

It seems that if an event handler is bound to it, the wx.ListBox
triggers the handler when it is being destroyed.

This is with Python 2.4.3, wxPython 2.8.7.1, Ubuntu Dapper.





More information about the wxpython-users mailing list