问题
I created frame with AuiNotebook and redefine EVT_AUINOTEBOOK_PAGE_CLOSE event where DeletePage is called:
def OnAuiNotebookPageClose( self, event ):
auinotebook = event.GetEventObject()
page_idx = event.GetSelection()
auinotebook.DeletePage(page_idx)
On click the "X" button on the page the event is invoked, but DeletePage always returned False, page was not removed... What is wrong in my code?
Please see my code below.
import wx
import wx.xrc
import wx.aui
class Frame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_auinotebook1 = wx.aui.AuiNotebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.aui.AUI_NB_DEFAULT_STYLE )
self.m_panel2 = wx.Panel( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_auinotebook1.AddPage( self.m_panel2, u"page1", True, wx.NullBitmap )
self.m_panel3 = wx.Panel( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_auinotebook1.AddPage( self.m_panel3, u"page2", False, wx.NullBitmap )
self.m_panel4 = wx.Panel( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_auinotebook1.AddPage( self.m_panel4, u"page3", False, wx.NullBitmap )
bSizer1.Add( self.m_auinotebook1, 1, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_auinotebook1.Bind( wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.OnAuiNotebookPageClose )
def OnAuiNotebookPageClose( self, event ):
auinotebook = event.GetEventObject()
page_idx = event.GetSelection()
auinotebook.DeletePage(page_idx)
if __name__ == "__main__":
app = wx.App(False)
frame = Frame1(None)
frame.CenterOnScreen()
frame.Show(True)
app.MainLoop()
回答1:
Bind the event to EVT_AUINOTEBOOK_PAGE_CLOSED
Not EVT_AUINOTEBOOK_PAGE_CLOSE
Use RemovePage
then DeletePage
i.e.:
self.m_auinotebook1.Bind( wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.OnAuiNotebookPageClose )
def OnAuiNotebookPageClose( self, event ):
auinotebook = event.GetEventObject()
page_idx = event.GetSelection()
auinotebook.RemovePage(page_idx)
auinotebook.DeletePage(page_idx)
来源:https://stackoverflow.com/questions/51875455/how-to-delete-page-of-auinotebook