Reputation: 5761
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)
Upvotes: 3
Views: 2085
Reputation: 1436
Very late and I am using wx.lib.agw.aui
. But perhaps is useful for someone else.
import wx
import wx.lib.agw.aui as aui
class MyForm(wx.Frame):
def __init__(self):
super().__init__(None, wx.ID_ANY, "1 & 2 do not close")
self.reportDown = aui.auibook.AuiNotebook(
self,
agwStyle=aui.AUI_NB_TOP|aui.AUI_NB_TAB_SPLIT|aui.AUI_NB_TAB_MOVE|aui.AUI_NB_SCROLL_BUTTONS|aui.AUI_NB_CLOSE_ON_ALL_TABS|aui.AUI_NB_MIDDLE_CLICK_CLOSE|aui.AUI_NB_DRAW_DND_TAB,
)
self.reportDown.AddPage(wx.Panel(self.reportDown), '1, I do not close')
self.reportDown.AddPage(wx.Panel(self.reportDown), '2, I do not close')
self.reportDown.AddPage(wx.Panel(self.reportDown), '3, I do close')
#--> For this to work you must include aui.AUI_NB_CLOSE_ON_ALL_TABS
#--> in the agwStyle of the AuiNotebook
# Remove close button from first tab
self.reportDown.SetCloseButton(0, False)
# Remove close button from second tab
self.reportDown.SetCloseButton(1, False)
self._mgr = aui.AuiManager()
self._mgr.SetManagedWindow(self)
self._mgr.AddPane(
self.reportDown,
aui.AuiPaneInfo(
).Center(
).Caption(
'1 & 2 do not close'
).Floatable(
b=False
).CloseButton(
visible=False
).Movable(
b=False
),
)
self._mgr.Update()
#---
#---
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
Upvotes: 1
Reputation: 1611
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)
Upvotes: 2