wxpython Segmentation Fault

In some of my wx python apps, I have come across this error.
It usually appears like this on the command line

Segmentation Fault (core dumped)

The most common reason for me getting this error, has been when a non-main thread tries to directly manipulate something in wx. Be it to Hide() a window, or ShowModal() a dialog.

The way to get round this (and actually, the way to use wx correctly, so I’m told), is to use the event handlers to pass messages back from your sub-processes to the wx event loop.

To create your own event class you can do this:

#First we'll define a new event type
wxEVT_THREAD_COM = wx.NewEventType()

#Now you can define your own event
class ThreadEvent(wx.PyEvent):
def __init__(self, id, data=None):
self.data = data
self.id = id

Once you’ve done that, you can create an event handler in the frame you want to pass the event to.

#now we'll create a handler in our main frame class
class MainFrame(wx.Frame):
def __init__(self):
self.Connect(-1, -1, wxEVT_THREAD_COM, self.OnThreadCommunicationEvent)

def OnThreadCommunicationEvent(self, event):
#handle the event here
if event.id==CLOSE:
elif event.id==HIDE:

To pass launch an event, you just need to get the event handler for the wx object, and then add the event!

#assuming MAIN_FRAME is a reference to the MAIN_FRAME in a foreign thread
myThreadEvent = ThreadEvent(CLOSE)

Leave a Reply

Your email address will not be published. Required fields are marked *