wxAuiNotebook - 阻止某些标签关闭

时间:2009-04-19 13:00:07

标签: wxpython

我正在尝试使用wx.aui.AuiNotebook;有没有办法阻止特定标签被关闭?即我有一个允许用户在AuiNotebook中创建多个标签的应用程序,但前两个标签是系统管理的,我不希望它们被关闭。

此外,在关闭事件中,我是否可以将附加到选项卡的窗口对象关闭? (从中提取数据)

2 个答案:

答案 0 :(得分:1)

我有类似的情况,我想阻止用户关闭最后一个标签。我所做的是绑定wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE事件,然后在事件处理程序中检查打开的选项卡数量。如果标签数小于2,我切换wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB样式,以便最后一个标签没有关闭按钮。

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)

答案 1 :(得分:1)

很晚了,我正在使用wx.lib.agw.aui。但是也许对其他人有用。

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()