I had a similar situation where I wanted to prevent the user from closing the last tab. What I did was binding the wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE
event and then in the event handler check for the number of tabs open. If the number of tabs is less than two I toggle the wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
style so that the last tab doesn't have a close button.
class MyAuiNotebook(wx.aui.AuiNotebook):
def __init__(self, *args, **kwargs):
kwargs['style'] = kwargs.get('style', wx.aui.AUI_NB_DEFAULT_STYLE) & \
~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
super(MyAuiNotebook, self).__init__(*args, **kwargs)
self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.onClosePage)
def onClosePage(self, event):
event.Skip()
if self.GetPageCount() <= 2:
# Prevent last tab from being closed
self.ToggleWindowStyle(wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
def AddPage(self, *args, **kwargs):
super(MyAuiNotebook, self).AddPage(*args, **kwargs)
# Allow closing tabs when we have more than one tab:
if self.GetPageCount() > 1:
self.SetWindowStyle(self.GetWindowStyleFlag() | \
wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)