Roy Tang

Programmer, engineer, scientist, critic, gamer, dreamer, and kid-at-heart.

Blog Notes Photos Links Archives About

I’m experimenting with wx.aui.AuiNotebook; is there a way I can prevent particular tabs from being closed? i.e. I have an app that allows the user to create multiple tabs in an AuiNotebook, but the first 2 tabs are system managed and I don’t want them to be closed.

Also, in a close event, can I get the window object attached to the tab being closed? (to extract data from it)

Comments

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)