我正在使用某些面板上的TextCtrlAutoComplete小部件修改wxpython应用程序。我销毁包含TextCtrlAutoComplete小部件的面板后有一个错误。似乎在令人厌恶之后仍然有一些活动的事件(特别是EVT_MOVE,EVT_SIZE)链接到TextCtrlAutoComplete.onControlChanged方法。这会导致死对象错误。所以,我的问题是有没有办法清理所有链接到特定对象方法的事件。现在TextCtrlAutoComplete将事件绑定到self以及上面的所有父母:
gp = self
while ( gp != None ) :
gp.Bind ( wx.EVT_MOVE , self.onControlChanged, gp )
gp.Bind ( wx.EVT_SIZE , self.onControlChanged, gp )
gp = gp.GetParent()
问题是,在调用Destroy()之后,这些事件仍处于活动状态!
以下是非常简单的应用程序,它展示了这个错误。
import wx
from wxext.TextCtrlAutoComplete import TextCtrlAutoComplete
########################################################################
class PanelOne(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.txt = wx.StaticText(self, label="Panel One")
self.txt.SetFont(wx.Font(14,wx.DEFAULT,wx.NORMAL,wx.BOLD))
self.button1 = wx.Button(self, id=-1, label='Button1', pos=(8, 8), size=(175, 28))
self.button1.Bind(wx.EVT_BUTTON, self.onButton1Click)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.txt, 1, flag=wx.EXPAND | wx.ALL, border=20)
self.sizer.Add(self.button1, 1, flag=wx.ALIGN_CENTER | wx.ALL, border=20)
self.SetSizer(self.sizer)
def onButton1Click(self,event):
if (self.GetParent().panelTwo):
self.SetBackgroundColour("lightblue")
self.GetParent().panelTwo.Destroy()
self.txt.SetLabel("Move of Size event lead to BUG")
self.SetSizer(self.sizer)
else:
self.SetBackgroundColour("lightgrey")
########################################################################
class PanelTwo(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.SetBackgroundColour("darkgreen")
self.txt = wx.StaticText(self, label="Panel Two")
self.txt.SetFont(wx.Font(14,wx.DEFAULT,wx.NORMAL,wx.BOLD))
self.searchbox = TextCtrlAutoComplete(self, choices=['No parameter found'])
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.txt, 1, flag=wx.ALIGN_CENTER | wx.ALL, border=20)
self.sizer.Add(self.searchbox, 1, flag=wx.ALIGN_CENTER | wx.ALL, border=20)
self.SetSizer(self.sizer)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Bug")
self.panelOne = PanelOne(self)
self.panelTwo = PanelTwo(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panelOne, 1, wx.EXPAND)
self.sizer.Add(self.panelTwo, 1, wx.EXPAND)
self.SetSizer(self.sizer)
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()