wxAuiNotebook-防止某些选项卡关闭

问题描述 投票:3回答:2

我正在尝试wx.aui.AuiNotebook;有什么方法可以防止关闭特定标签页吗?即,我有一个应用程序,允许用户在AuiNotebook中创建多个选项卡,但是前两个选项卡是系统管理的,我不希望它们被关闭。

而且,在关闭事件中,我可以将窗口对象附加到被关闭的选项卡上吗? (从中提取数据)

wxpython
2个回答
2
投票

我有类似的情况,我希望阻止用户关闭最后一个标签。我所做的是绑定wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE事件,然后在事件处理程序中检查打开的选项卡数。如果选项卡的数量少于两个,则我切换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)

0
投票

很晚,我正在使用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()
© www.soinside.com 2019 - 2024. All rights reserved.