Reputation: 4598
Using wxPython 2.8 on Arch (works fine, but I don't think it should...) and wxPython 2.9 on Mac OS X, both Python 2.7. My code consists of switching between different screens depending on the current states. On each switch between a state (screen) I clear my main Sizer and call DestroyChildren() on my main frame, then I populate them with other components.
So essentially I think I have outside threads trying to change wx components in the main thread. I'm guessing this is not best practise in wxPython, so how would I be able to amend this?
Attaching event to a button
self.login_button.Bind(wx.EVT_BUTTON, self.login_button_click)
The log in event does stuff and calls an update (the model state is changed here)
def login_button_click(self,event):
...
# CHANGE THE self.model.current_state
...
self.update(None)
The update clears the frame, checks state and calls a builder method
def update(self, o):
self.box.Clear() #clear the main sizer
self.frame.DestroyChildren() #delete all children, THIS IS WHERE THE CODE SEGFAULTS
print "STATE: "+self.model.current_state
if self.model.current_state == self.model....
...
elif self.model.current_state == self.model....
...
I have tried using wx.CallAfter, which resulted in all of the components beign removed at the end of the event, even the newly built ones.
Upvotes: 0
Views: 1231
Reputation: 33111
Are you actually using the threading module or anything related to threads? You really shouldn't need those to delete widgets in wxPython. I'm not sure why you're destroying all your widgets in the first place. Why not just hide the main frame and show a child frame with all the new widgets? Or use two panels. One panel is destroyed and a different panel with the other widgets is put in its place? I have an example of the latter on my blog.
Upvotes: 1