wx.事件处理程序中的DestroyChildren导致osx上的分段错误



以下Python代码在windows上运行良好,但在windows上导致分段错误osx。有什么建议吗?

使用CallAfter没有什么区别。
import wx
class myFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(wx.StaticText(self, -1, 'Static text'))
        self.sizer.Add(wx.Button(self, -1, 'Button'))
        self.SetSizer(self.sizer)
        self.Bind(wx.EVT_BUTTON, self.on_button)
    def on_button(self, event):
        self.sizer.Clear()
        self.DestroyChildren()
app = wx.App()
frame = myFrame()
frame.Show()
app.MainLoop()

这是因为仍然有系统消息等待被销毁的小部件,(在这种情况下鼠标移动)以及从事件处理程序返回后运行的代码将尝试使用被销毁的小部件(调用方法或访问属性)的可能性。

使用CallAfter延迟销毁确实为我解决了问题,你使用的是哪个版本的wxPython ?如果这对您不起作用,那么您可能需要尝试使用wx。用一个小的超时值代替CallLater。

import wx 
class myFrame(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None) 
        self.sizer = wx.BoxSizer(wx.VERTICAL) 
        self.sizer.Add(wx.StaticText(self, -1, 'Static text')) 
        self.sizer.Add(wx.Button(self, -1, 'Button')) 
        self.SetSizer(self.sizer) 
        self.Bind(wx.EVT_BUTTON, self.on_button) 
    def on_button(self, event): 
        wx.CallAfter(self.doit)
    def doit(self):
        self.sizer.Clear() 
        self.DestroyChildren() 
app = wx.App() 
frame = myFrame() 
frame.Show() 
app.MainLoop() 

最新更新