I'm trying to create 2 pages of wx.Notebook
(each page is wx.Panel
as a separate class). Class Main is wx.Frame
. And when I try to set the menus it has to be inside of wx.Frame
(Main class), however, my toolbars is in wx.Panel
(A class).
I print (z which of element A) in hide_show_f.
What I want is, while pressing Hide-Show Tool menu which is checkable, Toolbar 2 is should be hidden and then show. How to succeed in it. Thanks.
#!/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()
This is what the self
reference is used for, so that you don't have to redefine things again.
use:
self.page1 = A(nb)
self.page2 = B(nb)
make suitable amendments to page1 and page2 references
then:
def hide_show_f( self, event ):
if event.IsChecked():
print("hello")
print(self.page1.z)
self.page1._mgr.ClosePane(self.page1.aui_tb2)
self.page1._mgr.Update()
else:
print("bye")
or better:
def hide_show_f( self, event ):
if event.IsChecked():
self.page1.btn_1(None)
else:
self.page1.btn_2(None)