Mar-17-2019, 11:58 AM
Hi guys!
I have the following code, everything is working, except that I can't get the function for hiding the tool bar to work. Originally I was using a different approach from a tutorial, it was working, but now that I added new things and changed some old, I get a problem.
Also a good python function reference would be good.
Thanks in advance!
Important part of the code, new appraoch:
I have the following code, everything is working, except that I can't get the function for hiding the tool bar to work. Originally I was using a different approach from a tutorial, it was working, but now that I added new things and changed some old, I get a problem.
error: AttributeError: 'Example' object has no attribute 'toolbar'I tried changing the function "toolbar.Hide()" with "toolbar = toolbar.Hide()" and "toolbar = Hide()" and a few other things, but it does not work. Can someone explain to me the difference between the 2 approaches and how to fix the problem?
Also a good python function reference would be good.
Thanks in advance!
Important part of the code, new appraoch:
#Toolbar block. toolbar = self.CreateToolBar()#Visual creation of the toolbar. QuitTool = toolbar.AddTool(wx.ID_ANY, 'Exit', wx.Bitmap('exit.png'))#Adding picture to the toolbar button. OpenTool = toolbar.AddTool(wx.ID_ANY, 'Open', wx.Bitmap('o.png'))#Adding picture to the toolbar button. toolbar.Realize() self.Bind(wx.EVT_TOOL, self.OnQuit, QuitTool)#Adding operation to the toolbar button. self.Bind(wx.EVT_TOOL, self.OnQuit, OpenTool)#Adding operation to the toolbar button. .......................... def ToggleToolBar(self, e):#Function for the menu function block. Different toolbar. For ticking on/of the toolbar. if self.shtl.IsChecked(): toolbar.Show() else: toolbar.Hide()Important part of the code old approach.
self.toolbar = self.CreateToolBar()#Visual creation of the toolbar. self.toolbar.AddTool(1, '', wx.Bitmap('exit.png'))#Adding picture to the toolbar button. self.toolbar.Realize()#Adding operation to the toolbar button. ......................... def ToggleToolBar(self, e):#Function for the menu function block. if self.shtl.IsChecked(): self.toolbar.Show() else: self.toolbar.Hide()Full code, new approach:
#!/usr/bin/env python3 #Settings. # -*- coding: utf-8 -*- #Encoding of the source code. # simple.py #Name of the file. import wx APP_EXIT = 1#ID APP_SECOND = 2#ID #Context menu visual and operation creation. class MyPopupMenu(wx.Menu): def __init__(self, parent): super(MyPopupMenu, self).__init__() self.parent = parent #Minimize block. mmi = wx.MenuItem(self, wx.NewId(), 'Minimize') self.Append(mmi) self.Bind(wx.EVT_MENU, self.OnMinimize, mmi) #Close block. cmi = wx.MenuItem(self, wx.NewId(), 'Close') self.Append(cmi) self.Bind(wx.EVT_MENU, self.OnClose, cmi) #Custom block. Custom = wx.MenuItem(self, wx.NewId(), 'Do')#Create the visual style. self.Append(Custom)#Add the visual style to the context menu. self.Bind(wx.EVT_MENU, self.OnDo, Custom)#Add the operation to the context menu item. def OnMinimize(self, e):#Minimize operation self.parent.Iconize() def OnClose(self, e):#Close operation self.parent.Close() def OnDo(self, e):#Custom operation self.parent.Close() class Example(wx.Frame):#"wx.Frame" is turned into a class. def __init__(self, *args, **kwargs):#???Initialization??? super(Example, self).__init__(*args, **kwargs) self.InitUI() def InitUI(self):#Main features. self.locale = wx.Locale(wx.LANGUAGE_ENGLISH) menubar = wx.MenuBar() viewMenu = wx.Menu() #Menu visual button creation block. #Example: ID = viewMenu.Append(wx.ID_ANY, 'Name', 'Name', kind=wx.TYPE_OF_MENU) self.shst = viewMenu.Append(wx.ID_ANY, 'Show statusbar', 'Show Statusbar', kind=wx.ITEM_CHECK)#Add a check item. Parameters can be used for any type of menu command. The ID only has to be different. self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar', 'Show Toolbar', kind=wx.ITEM_CHECK)#Add a check item. Parameters can be used for any type of menu command. The ID only has to be different. self.quit = viewMenu.Append(wx.ID_ANY, 'Exit', 'Exit', kind=wx.ITEM_CHECK)#Add a check item. Parameters can be used for any type of menu command. The ID only has to be different. #Menu button additional functions block. viewMenu.Check(self.shst.GetId(), True)#Add a tick possibility to the menu option. Function is on, when the tick is on. viewMenu.Check(self.shtl.GetId(), True)#Add a tick possibility to the menu option. Function is on, when the tick is on. #Menu function block. self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)#Specify which funtion to be performed when the menu is ticked on/off. self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl)#Specify which funtion to be performed when the menu is ticked on/off. self.Bind(wx.EVT_MENU, self.OnQuit, self.quit)#Specify which funtion to be performed when the menu is ticked on/off. This menu has no tick option, it will only work once for off, #no on option by putting the tick again is possible, because "viewMenu.Chech()" is not added for it. For "EXIT" its not needed. menubar.Append(viewMenu, '&Menu')#Name of the menu button shown in the up toolbar. self.SetMenuBar(menubar) #Toolbar block. toolbar = self.CreateToolBar()#Visual creation of the toolbar. QuitTool = toolbar.AddTool(wx.ID_ANY, 'Exit', wx.Bitmap('exit.png'))#Adding picture to the toolbar button. OpenTool = toolbar.AddTool(wx.ID_ANY, 'Open', wx.Bitmap('o.png'))#Adding picture to the toolbar button. toolbar.Realize() self.Bind(wx.EVT_TOOL, self.OnQuit, QuitTool)#Adding operation to the toolbar button. self.Bind(wx.EVT_TOOL, self.OnQuit, OpenTool)#Adding operation to the toolbar button. #Status bar block. self.statusbar = self.CreateStatusBar()#Visual creation of the statusbar. self.statusbar.SetStatusText('Ready')#Default text displayed in the statusbar. #Program size/title/position block. self.SetSize((350, 250))#Set the program size. self.SetTitle('Toolbox')#Set the program title in the upest bar. self.Centre()#The program will start in the center of the screen. #Context menu block. self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)#On "RightDown"(right-click) event, do RightDown operation. def OnRightDown(self, e):#Context menu right click operation.#Operation RightDown. self.PopupMenu(MyPopupMenu(self), e.GetPosition())#Menu to be shown=MyPopupMenu(self), Position to be shown=e.GetPosition. def ToggleStatusBar(self, e):#Function for the menu function block. if self.shst.IsChecked(): self.statusbar.Show() else: self.statusbar.Hide() def ToggleToolBar(self, e):#Function for the menu function block. Different toolbar. For ticking on/of the toolbar. if self.shtl.IsChecked(): self.toolbar.Show() else: self.toolbar.Hide() def OnQuit(self, e):#Function for the menu function block. self.Close() def main(): app = wx.App() ex = Example(None) ex.Show() app.MainLoop() if __name__ == '__main__': main()