WxPython关闭标签或笔记本时显示和隐藏auinotebook

时间:2019-02-02 01:39:29

标签: python-3.x wxpython

您好,我使用wxformbuilder创建了一个设计,它由 wx.auinotebook 的两个选项卡和一个带有打开已关闭的选项卡的菜单栏组成。

import wx
import wx.aui

wx.ID_ShPn1 = 1000

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

        self.m_menubar1 = wx.MenuBar( 0 )
        self.m_menu5 = wx.Menu()
        self.m_menu2 = wx.Menu()
        self.m_menuItem1 = wx.MenuItem( self.m_menu2, wx.ID_ShPn1, u"panel1", wx.EmptyString, wx.ITEM_NORMAL )
        self.m_menu2.Append( self.m_menuItem1 )

        self.m_menu5.AppendSubMenu( self.m_menu2, u"Show" )

        self.m_menubar1.Append( self.m_menu5, u"Inicio" )

        self.SetMenuBar( self.m_menubar1 )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_splitter1 = wx.SplitterWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_3D )
        self.m_splitter1.Bind( wx.EVT_IDLE, self.m_splitter1OnIdle )

        self.m_panel1 = wx.Panel( self.m_splitter1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer2 = wx.BoxSizer( wx.VERTICAL )

        self.m_auinotebook1 = wx.aui.AuiNotebook( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.aui.AUI_NB_DEFAULT_STYLE|wx.aui.AUI_NB_MIDDLE_CLICK_CLOSE )
        self.m_auinotebook1.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )

        self.m_panel2 = wx.Panel( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_panel2.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_GRAYTEXT ) )

        self.m_auinotebook1.AddPage( self.m_panel2, u"a page", True, wx.NullBitmap )
        self.m_panel4 = wx.Panel( self.m_auinotebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_auinotebook1.AddPage( self.m_panel4, u"panel 1", False, wx.NullBitmap )

        bSizer2.Add( self.m_auinotebook1, 1, wx.EXPAND |wx.ALL, 5 )


        self.m_panel1.SetSizer( bSizer2 )
        self.m_panel1.Layout()
        bSizer2.Fit( self.m_panel1 )
        self.m_splitter1.Initialize( self.m_panel1 )
        bSizer1.Add( self.m_splitter1, 1, wx.EXPAND, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()
        self.m_statusBar1 = self.CreateStatusBar( 1, wx.STB_SIZEGRIP, wx.ID_ANY )

        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_MENU, self.Show_panel, id = self.m_menuItem1.GetId() )

    def __del__( self ):
        pass

    # Virtual event handlers, overide them in your derived class

    def Show_panel( self, event ):
        event.Skip() 

    def m_splitter1OnIdle( self, event ):
        self.m_splitter1.SetSashPosition( 0 )
        self.m_splitter1.Unbind( wx.EVT_IDLE )

if __name__ == "__main__":
        app = wx.App(False)
        frame = MyFrame1(None)
        frame.Show()
        app.MainLoop()

在此代码中,我要使其显示我已关闭的选项卡。

    def Show_panel( self, event ):
        event.Skip() 

我可以使用什么代码,以便在关闭标签页或笔记本时可以再次打开

1 个答案:

答案 0 :(得分:1)

我建议您先看看this question about how to delete AuiNotebook pages

但是,该问题与您的问题有一个很大的区别:您希望能够在关闭页面后重新添加该页面。如果您查看该解决方案,则可以看到它们同时调用AuiNotebook.RemovePage()AuiNotebook.DeletePage()。查看这两个功能的文档,很明显,虽然您确实想删除该页面,但不想删除它。因此,只要您不打DeletePage(),您以后就应该可以轻松地使用AddPage()

重新添加页面。