By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

General help with hide show toolbar for a begineer in python

P: 1
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.

Expand|Select|Wrap|Line Numbers
  1. error: AttributeError: 'Example' object has no attribute 'toolbar'
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1.                                     #Toolbar block.
  2. toolbar = self.CreateToolBar()#Visual creation of the toolbar.
  3. QuitTool = toolbar.AddTool(wx.ID_ANY, 'Exit', wx.Bitmap('exit.png'))#Adding picture to the toolbar button.
  4. OpenTool = toolbar.AddTool(wx.ID_ANY, 'Open', wx.Bitmap('o.png'))#Adding picture to the toolbar button.
  5. toolbar.Realize()
  6.  
  7. self.Bind(wx.EVT_TOOL, self.OnQuit, QuitTool)#Adding operation to the toolbar button.
  8. self.Bind(wx.EVT_TOOL, self.OnQuit, OpenTool)#Adding operation to the toolbar button.
  9. ..........................
  10. def ToggleToolBar(self, e):#Function for the menu function block. Different toolbar. For ticking on/of the toolbar.
  11. if self.shtl.IsChecked():
  12.     toolbar.Show()
  13. else:
  14.     toolbar.Hide() 
  15.  
Important part of the code old approach.
Expand|Select|Wrap|Line Numbers
  1. self.toolbar = self.CreateToolBar()#Visual creation of the toolbar.
  2. self.toolbar.AddTool(1, '', wx.Bitmap('exit.png'))#Adding picture to the toolbar button.
  3. self.toolbar.Realize()#Adding operation to the toolbar button.
  4. .........................
  5. def ToggleToolBar(self, e):#Function for the menu function block.
  6. if self.shtl.IsChecked():
  7.     self.toolbar.Show()
  8. else:
  9.     self.toolbar.Hide()
  10.  
Full code, new approach:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python3  #Settings.
  2. # -*- coding: utf-8 -*- #Encoding of the source code.
  3.  
  4. # simple.py #Name of the file.
  5.  
  6.  
  7. import wx
  8.  
  9. APP_EXIT = 1#ID
  10. APP_SECOND = 2#ID
  11.                             #Context menu visual and operation creation.
  12. class MyPopupMenu(wx.Menu):
  13.  
  14.     def __init__(self, parent):
  15.         super(MyPopupMenu, self).__init__()
  16.  
  17.         self.parent = parent
  18.                                                         #Minimize block.
  19.         mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
  20.         self.Append(mmi)
  21.         self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
  22.                                                     #Close block.
  23.         cmi = wx.MenuItem(self, wx.NewId(), 'Close')
  24.         self.Append(cmi)
  25.         self.Bind(wx.EVT_MENU, self.OnClose, cmi)
  26.                                                 #Custom block.
  27.         Custom = wx.MenuItem(self, wx.NewId(), 'Do')#Create the visual style.
  28.         self.Append(Custom)#Add the visual style to the context menu.
  29.         self.Bind(wx.EVT_MENU, self.OnDo, Custom)#Add the operation to the context menu item.
  30.  
  31.     def OnMinimize(self, e):#Minimize operation
  32.         self.parent.Iconize()
  33.  
  34.     def OnClose(self, e):#Close operation
  35.         self.parent.Close()
  36.  
  37.     def OnDo(self, e):#Custom operation
  38.         self.parent.Close()
  39.  
  40. class Example(wx.Frame):#"wx.Frame" is turned into a class.
  41.  
  42.     def __init__(self, *args, **kwargs):#???Initialization???
  43.         super(Example, self).__init__(*args, **kwargs)
  44.  
  45.         self.InitUI()
  46.  
  47.     def InitUI(self):#Main features.
  48.         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
  49.         menubar = wx.MenuBar()
  50.         viewMenu = wx.Menu()
  51.                                                                 #Menu visual button creation block.
  52.                                                                 #Example: ID = viewMenu.Append(wx.ID_ANY, 'Name', 'Name', kind=wx.TYPE_OF_MENU)
  53.         self.shst = viewMenu.Append(wx.ID_ANY, 'Show statusbar',
  54.             '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.
  55.         self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar',
  56.             '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.
  57.         self.quit = viewMenu.Append(wx.ID_ANY, 'Exit',
  58.             '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.
  59.                                                #Menu button additional functions block.
  60.         viewMenu.Check(self.shst.GetId(), True)#Add a tick possibility to the menu option. Function is on, when the tick is on.
  61.         viewMenu.Check(self.shtl.GetId(), True)#Add a tick possibility to the menu option. Function is on, when the tick is on.
  62.                                                                 #Menu function block.
  63.         self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)#Specify which funtion to be performed when the menu is ticked on/off.
  64.         self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl)#Specify which funtion to be performed when the menu is ticked on/off.
  65.         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,
  66.                                                       #no on option by putting the tick again is possible, because "viewMenu.Chech()" is not added for it. For "EXIT" its not needed.
  67.  
  68.         menubar.Append(viewMenu, '&Menu')#Name of the menu button shown in the up toolbar.
  69.         self.SetMenuBar(menubar)
  70.                                             #Toolbar block.
  71.         toolbar = self.CreateToolBar()#Visual creation of the toolbar.
  72.         QuitTool = toolbar.AddTool(wx.ID_ANY, 'Exit', wx.Bitmap('exit.png'))#Adding picture to the toolbar button.
  73.         OpenTool = toolbar.AddTool(wx.ID_ANY, 'Open', wx.Bitmap('o.png'))#Adding picture to the toolbar button.
  74.         toolbar.Realize()
  75.  
  76.         self.Bind(wx.EVT_TOOL, self.OnQuit, QuitTool)#Adding operation to the toolbar button.
  77.         self.Bind(wx.EVT_TOOL, self.OnQuit, OpenTool)#Adding operation to the toolbar button.
  78.                                                 #Status bar block.
  79.         self.statusbar = self.CreateStatusBar()#Visual creation of the statusbar.
  80.         self.statusbar.SetStatusText('Ready')#Default text displayed in the statusbar.
  81.                                 #Program size/title/position block.
  82.         self.SetSize((350, 250))#Set the program size.
  83.         self.SetTitle('Toolbox')#Set the program title in the upest bar.
  84.         self.Centre()#The program will start in the center of the screen.
  85.                                                     #Context menu block.
  86.         self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)#On "RightDown"(right-click) event, do RightDown operation.
  87.  
  88.     def OnRightDown(self, e):#Context menu right click operation.#Operation RightDown.
  89.         self.PopupMenu(MyPopupMenu(self), e.GetPosition())#Menu to be shown=MyPopupMenu(self), Position to be shown=e.GetPosition.
  90.  
  91.     def ToggleStatusBar(self, e):#Function for the menu function block.
  92.  
  93.         if self.shst.IsChecked():
  94.             self.statusbar.Show()
  95.         else:
  96.             self.statusbar.Hide()
  97.  
  98.     def ToggleToolBar(self, e):#Function for the menu function block. Different toolbar. For ticking on/of the toolbar.
  99.  
  100.         if self.shtl.IsChecked():
  101.             self.toolbar.Show()
  102.         else:
  103.             self.toolbar.Hide()
  104.  
  105.     def OnQuit(self, e):#Function for the menu function block.
  106.         self.Close()
  107.  
  108.  
  109. def main():
  110.  
  111.     app = wx.App()
  112.     ex = Example(None)
  113.     ex.Show()
  114.     app.MainLoop()
  115.  
  116.  
  117. if __name__ == '__main__':
  118.     main()
  119.  
Mar 17 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.