我正在尝试创建 2 页wx.Notebook
(每个页面都是wx.Panel
一个单独的类)。主类是wx.Frame
. 当我尝试设置菜单时,它必须位于wx.Frame
(Main类)中,但是,我的工具栏位于wx.Panel
(A类)中。
我在 hide_show_f 中打印 (z which of element A)。
我想要的是,在按下可检查的隐藏显示工具菜单时,工具栏 2 应该被隐藏然后显示。如何在其中取得成功。谢谢。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import wx
import wx.lib.agw.aui as aui
class Main ( wx.Frame ): ### Background program which have real-time and text notebook-panels separately
def __init__( self, parent ):
self.parent = parent
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Main Frame", pos = wx.DefaultPosition, size = wx.Size( 500,350 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
p = wx.Panel(self)
nb = wx.Notebook(p)
page1 = A(nb)
page2 = B(nb)
nb.AddPage(page1, "A")
nb.AddPage(page2, "B")
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
###### view menu
self.menubar = wx.MenuBar( 0 )
self.view_menu = wx.Menu()
self.connecttool_menuItem = wx.MenuItem( self.view_menu, wx.ID_ANY, u"Hide-Show Tool", wx.EmptyString, wx.ITEM_NORMAL )
self.view_menu.AppendCheckItem( self.connecttool_menuItem.GetId(), 'Hide-Show Tool')
self.menubar.Append( self.view_menu, u"View" )
self.SetMenuBar( self.menubar )
#Bind
self.Bind( wx.EVT_MENU, self.hide_show_f, id = self.connecttool_menuItem.GetId() )
def hide_show_f( self, event ):
if event.IsChecked():
print("hello")
f = A(self)
print(f.z)
f._mgr.ClosePane(f.aui_tb2)
f._mgr.Update()
else:
print("bye")
class A ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent )
self.z = 54
self.bSizer1 = wx.BoxSizer( wx.VERTICAL )
### auitoolbar 1
self.tb1 = aui.AuiToolBar(self, -1, wx.DefaultPosition, wx.DefaultSize, agwStyle=aui.AUI_TB_DEFAULT_STYLE | aui.AUI_TB_OVERFLOW)
self.Btn1 = wx.Button( self.tb1, wx.ID_ANY, u"Hide", wx.DefaultPosition, wx.DefaultSize, 0 )
self.tb1.AddControl( self.Btn1 )
self.Btn2 = wx.Button( self.tb1, wx.ID_ANY, u"Show", wx.DefaultPosition, wx.DefaultSize, 0 )
self.tb1.AddControl( self.Btn2 )
self.tb1.Realize()
self.bSizer1.Add( self.tb1, 0, 0, 5 )
### auitoolbar 2
self.tb2 = aui.AuiToolBar(self, -1, wx.DefaultPosition, wx.DefaultSize, agwStyle=aui.AUI_TB_DEFAULT_STYLE | aui.AUI_TB_OVERFLOW)
self.Btn3 = wx.Button( self.tb2, wx.ID_ANY, u"Button", wx.DefaultPosition, wx.DefaultSize, 0 )
self.tb2.AddControl( self.Btn3 )
self.tb2.Realize()
self.bSizer1.Add( self.tb2, 0, 0, 5 )
### aui manager
self._mgr = aui.AuiManager()
self._mgr.SetManagedWindow(self)
self.aui_tb1 = aui.AuiPaneInfo().Name("tb1").Caption("Hide-Show Tool").ToolbarPane().Top()
self.aui_tb2 = aui.AuiPaneInfo().Name("tb2").Caption("Btn Tool").ToolbarPane().Top()
self._mgr.AddPane(self.tb1, self.aui_tb1)
self._mgr.AddPane(self.tb2, self.aui_tb2)
self._mgr.Update()
self.Btn1.Bind( wx.EVT_BUTTON, self.btn_1 )
self.Btn2.Bind( wx.EVT_BUTTON, self.btn_2 )
self.Btn3.Bind( wx.EVT_BUTTON, self.btn_3 )
def btn_1(self, event):
self._mgr.ClosePane(self.aui_tb2)
self._mgr.Update()
def btn_2(self, event):
self._mgr.ShowPane(self.tb2, self.aui_tb2)
self._mgr.Update()
def btn_3(self, event):
print("hello")
class B ( wx.Panel ):
def __init__( self, parent ):
wx.Panel.__init__ ( self, parent )
if __name__ == '__main__':
app = wx.App()
ex = Main(None)
ex.Show()
app.MainLoop()