[wxPython-users] Deleting a text control from an EVT_TEXT_ENTER
Werner F. Bruhin
werner.bruhin at free.fr
Wed Jul 18 00:09:13 PDT 2007
Hi Anthony,
Anthony M. Floyd wrote:
> Here's something that's been confounding one of our developers here:
>
> We have a wx.ListBox control, and want to provide in-placed editing of
> certain parts of the text displayed in each row. The way the dev has
> done it is to capture a double click on the sensitive part of the text,
> and dynamically overlay a wx.TextCtrl to provide the editing at that
> location. This works OK (I suspect we're going to move to some sort of
> custom control in the future) but he is having a problem destroying the
> text control afterwards. Well, *sometimes* there's a problem.
>
> When the text control is created, an event handler is bound to the
> parent to handle the EVT_TEXT_ENTER event. The corresponding event
> handler does a myTextControl.Destroy(). The idea is when the data is
> entered in the control and the user presses enter, the data is
> "recorded" and the editing box disappears.
>
> This causes a hard crash (no errors, no "unhandled exceptions" in the
> debugger).
>
> I've attached sample code that duplicates the problem pretty easily.
> WinXP, wxPython 2.8.3.0, Python 2.5.1.
>
> What is causing the crash? I suspect there's another event that follows
> the EVT_TEXT_ENTER event, but I don't understand why this isn't handled
> cleanly by the "safe" .Destroy() method.
>
> How can we deal with this? Anyone have any suggestions? At the moment,
> rather than running Destroy(), the controls are being hidden and
> appended to a list in the parent, and during an EVT_IDLE handler the
> controls in this list are destroyed. This works ok, but doesn't seem
> "clean".
>
Detaching it from the sizer and then destroying seems to work, e.g.:
def _onEnter(self, event):
## self.textCtrl.Destroy()
self.DestroyTextCtrl(self.textCtrl)
#
# commenting out the event.Skip avoids the crash
# in this case, but in our full application, the crash
# occurs anyway, presumably due to other operations occuring
# in the event handler
#
event.Skip()
def DestroyTextCtrl(self, control):
sizer = control.GetContainingSizer()
if sizer:
sizer.Detach(control)
wx.CallAfter(control.Destroy)
Werner
More information about the wxpython-users
mailing list