views:

40

answers:

1

HI, guys

I am developing a GUI with wxPython. I draw a square which represents a CD object, inside another square (also with wxPanel class), which represents CD Container Object. I want to have "delete this CD" in the right click menu of CDWindow, which will remove the CDwindow. enter code here` Basically, my code looks like this (for simplicity, I keep the main parts):

class CDContainerWindow(wx.Panel):    
   def __init__(self):
       wx.Panel.__init__(self, parent, id, pos, size)  
       cd_win=CDWindow()

class CDWindow(wx.Panel):  
   def __init__(self):
      wx.Panel.__init__(self, parent, id, pos, size)
      self.Bind(wx.EVT_MENU, self.OnDeleteCD, item_CD)

   def OnDeleteCD(self, event):
      self.destroy()

There is an error message "Segmentation fault" what is wrong with my way? How can I delete this CD window from the CDContainer Window?

enter code here

+1  A: 

Maybe there's a sizer still using the destroyed panel? You should remove the panel from the sizer first.

Joril
HI, Joril, thank you for your answer. I did not use any sizer. I just include the cdwindow as a variable. I do not know if it is possible to ask an object to destroy itself.
pepero
I see.. Does the segfault happen immediately on destruction, or on exiting OnDeleteCD?
Joril
hi, Joril, Thanks again for your follow-up. I really appreciate this big help. it was happening exactly when I called self.Destroy(). Just one thing, in the OnPaint function of this CDWindow class, I use DC to draw some circles, square, and text. And when I called OnDeleteCD, I only called self.Destroy(). Besides, I put print("test") before and after this self.Destroy(), and both can be output to the console, then there is segfault message. This is bit wierd. because usually python gives me enough information on where the problem is. but this time, i only get this segfault, nothing else.
pepero
I'd try calling wx.CallAfter(self.Destroy) instead of self.Destroy()... If it still segfaults, post a runnable example please :)
Joril
Hi, Joril, You are my hero!! It works with wx.CallAfwx.CallAfterter(self.Destroy). Could you please explain me what the problem is and how wx.CallAfter could solve this problem? Thank you very much for your great help!!!
pepero
Well my hypothesis was that wxPython was still trying to access the destroyed panel during the current event handler, and wasn't expecting it to be gone.. wx.CallAfter ensures that the function you pass as parameter gets called AFTER the current event handler has finished its job :)
Joril