0

I want to dynamically create and delete pages in a notebook. In the main class I successfully create and add pages with a button. The pages are a separate class of course, and have a button inside. I know I can put the button outside the notebook and delete them from the main class but I want to use page's own button to self destruct the whole page as it won't be needed any more.

Sorry I don't post any code but I'm posting from my cellphone. Also it seems to be a question not so specific to need a minimal working example.

Javier
  • 801
  • 3
  • 10
  • 24

1 Answers1

0

As you've guessed, care needs to be taken when destroying UI objects from their own event handlers. Not only is the current event handler still active, but there may be other pending events that are still in the queue, and if the target object has been destroyed when they are delivered then you can get a crash.

The best thing to do is to defer the destruction until after the current and possible pending event handlers have been completed and there is nothing waiting to be done on the UI object except for the destruction that you want to do. And the best way to do that is to use wx.CallAfter. It will call a function with parameters that you give it the next time the the event loop empties, so implicitly there is not anything else waiting to be done or sent to the UI object in question.

In your case it would be safe to do things like immediately remove the page from the notebook, and hide the page window. Then use wx.CallAfter to call some function (perhaps in the notebook class) which calls the page window's Destroy method and does any other cleanup that is necessary. The reason I suggest splitting these two sets of tasks is not because it will take a long time for the function to be called, but in some cases it may be long enough to flicker momentarily in a transitory state, so the appearance is less smooth for the users.

RobinDunn
  • 6,116
  • 1
  • 15
  • 14