PyDeadObjectError(self.attrStr % self._name) ?

Hi guys,

I am pretty new to wxPython, but there is something I do not
understand. This is the code I am using to create a dialog;

    def OnBackupItem(self, event): # wxGlade: ArchivisionFrm.<event_handler>
        dlg = ar_dlg.ArchiverDlg(self, -1)
        dlg.ShowModal()
        dlg.Destroy()
        event.Skip()

Looks pretty ok, and it works in a handler for a wxButton. If I
execute the same code from a menu handler, I get a dead object error:

    raise PyDeadObjectError(self.attrStr % self._name)
wx._core.PyDeadObjectError: The C++ part of the ArchiverMgtDlg object
has been deleted, attribute access no longer allowed.

The wxFrame is generated by wxGlade, and the menu event seems to get
fired, however I cannot instantiate a dialog from that menu handler.

The code is here: http://www.xs4all.nl/~jorgb/dead_object.zip

wxGlade project is included, if you fire up Archivision.py and select
from the file menu -> Archivers .. the same code is executed as
pressing the [Backup] button. One works, the other crashes.

wxPython 2.8.0 Unicode Debug Build

Does anyone have any idea? I was going along so well, but I am puzzled
as I am not seeing what I am doing wrong..

- Jorgen

Ok I figured it out!

It is a wxGlade problem. I defined a menu like;

&Test----- ID = -1 ----- OnTest

And I noticed that wxGlade makes this code;

        self.FmMain_menubar = wx.MenuBar()
        self.SetMenuBar(self.FmMain_menubar)
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(wx.NewId(), "&Test", "", wx.ITEM_NORMAL)
        self.FmMain_menubar.Append(wxglade_tmp_menu, "&File")

HOWEVER, during event binding I see this;

        self.Bind(wx.EVT_MENU, self.OnTest, id=-1)

And AFAIK you cannot bind to an ID -1. The menus behaved erratic,
sometimes they were ignored most of the times they resulted in a
crash.

wxGlade's solution would be binding to the event given to
wxglade_tmp_menu but I think that one is overwritten for every new
menu item.

I will try to post this as well on the wxGlade mailinglist. It took me
a day of confusion until I found out that ALL menus did not work with
an auto generated ID in wxGlade..

Regards,
- Jorgen

···

On 2/16/07, Jorgen Bodde <jorgen.maillist@gmail.com> wrote:

Hi guys,

I am pretty new to wxPython, but there is something I do not
understand. This is the code I am using to create a dialog;

    def OnBackupItem(self, event): # wxGlade: ArchivisionFrm.<event_handler>
        dlg = ar_dlg.ArchiverDlg(self, -1)
        dlg.ShowModal()
        dlg.Destroy()
        event.Skip()

Looks pretty ok, and it works in a handler for a wxButton. If I
execute the same code from a menu handler, I get a dead object error:

    raise PyDeadObjectError(self.attrStr % self._name)
wx._core.PyDeadObjectError: The C++ part of the ArchiverMgtDlg object
has been deleted, attribute access no longer allowed.

The wxFrame is generated by wxGlade, and the menu event seems to get
fired, however I cannot instantiate a dialog from that menu handler.

The code is here: http://www.xs4all.nl/~jorgb/dead_object.zip

wxGlade project is included, if you fire up Archivision.py and select
from the file menu -> Archivers .. the same code is executed as
pressing the [Backup] button. One works, the other crashes.

wxPython 2.8.0 Unicode Debug Build

Does anyone have any idea? I was going along so well, but I am puzzled
as I am not seeing what I am doing wrong..

- Jorgen

Jorgen Bodde wrote:

Ok I figured it out!

It is a wxGlade problem. I defined a menu like;

&Test----- ID = -1 ----- OnTest

And I noticed that wxGlade makes this code;

       self.FmMain_menubar = wx.MenuBar()
       self.SetMenuBar(self.FmMain_menubar)
       wxglade_tmp_menu = wx.Menu()
       wxglade_tmp_menu.Append(wx.NewId(), "&Test", "", wx.ITEM_NORMAL)
       self.FmMain_menubar.Append(wxglade_tmp_menu, "&File")

HOWEVER, during event binding I see this;

       self.Bind(wx.EVT_MENU, self.OnTest, id=-1)

And AFAIK you cannot bind to an ID -1. The menus behaved erratic,
sometimes they were ignored most of the times they resulted in a
crash.

-1 is allowed, but it just means that the binding will match a menu event from any menu item. IOW, -1 is a wildcard.

wxGlade's solution would be binding to the event given to
wxglade_tmp_menu but I think that one is overwritten for every new
menu item.

What it should be doing is something like this:

         wxglade_tmp_menu = wx.Menu()
         testItem = wxglade_tmp_menu.Append(wx.NewId(), "&Test", "", wx.ITEM_NORMAL)
         self.FmMain_menubar.Append(wxglade_tmp_menu, "&File")

         self.Bind(wx.EVT_MENU, self.OnTest, testItem)

Or use a specific ID in the Append, and also in the Bind with the id parameter.

···

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

Hi Robin,

Agreed. So the code generation on wxGlade needs some improvement on
the menu side. It really behaved weird giving random errors or even
unhandled menu events.

- Jorgen

···

On 2/19/07, Robin Dunn <robin@alldunn.com> wrote:

Jorgen Bodde wrote:
> Ok I figured it out!
>
> It is a wxGlade problem. I defined a menu like;
>
> &Test----- ID = -1 ----- OnTest
>
> And I noticed that wxGlade makes this code;
>
> self.FmMain_menubar = wx.MenuBar()
> self.SetMenuBar(self.FmMain_menubar)
> wxglade_tmp_menu = wx.Menu()
> wxglade_tmp_menu.Append(wx.NewId(), "&Test", "", wx.ITEM_NORMAL)
> self.FmMain_menubar.Append(wxglade_tmp_menu, "&File")
>
> HOWEVER, during event binding I see this;
>
> self.Bind(wx.EVT_MENU, self.OnTest, id=-1)
>
> And AFAIK you cannot bind to an ID -1. The menus behaved erratic,
> sometimes they were ignored most of the times they resulted in a
> crash.

-1 is allowed, but it just means that the binding will match a menu
event from any menu item. IOW, -1 is a wildcard.

>
> wxGlade's solution would be binding to the event given to
> wxglade_tmp_menu but I think that one is overwritten for every new
> menu item.

What it should be doing is something like this:

         wxglade_tmp_menu = wx.Menu()
         testItem = wxglade_tmp_menu.Append(wx.NewId(), "&Test", "",
wx.ITEM_NORMAL)
         self.FmMain_menubar.Append(wxglade_tmp_menu, "&File")

         self.Bind(wx.EVT_MENU, self.OnTest, testItem)

Or use a specific ID in the Append, and also in the Bind with the id
parameter.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org