473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

General help with hide show toolbar for a begineer in python

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
0 1121

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

Similar topics

9
by: Robb Gilmore | last post by:
Hello, This is probably an easy one, but I have not been able to figure it out so far. I have a tab control on a windows forms app. Depending on some business logic, I need to hide/show some...
2
by: Sandra | last post by:
Hello, I want to develop an application for WindowsCE using eMbedded Visual C++ 4.0. Is it possible to add/delete or hide/show toolbars by running the application. e.g. the user choose a menuitem...
4
by: jerryyang_la1 | last post by:
I've found this script that allows be to hide/show form elements.. <script language="JavaScript"><!-- var toggle = true; function show(object) { if (document.layers && document.layers)...
1
by: Norman | last post by:
Hello All, I have developed the ASP.Net application using .Net 1.1. I want to get one section of the page under hide/show feauture. That means when the user clicks on display I want to display all...
2
by: Greg | last post by:
Hello, I am trying to display order ids and order details (order items). I would like to give the user Hide/Show option to either display or hide order details. The page would look like: ...
1
by: roni | last post by:
hi. i have webform with 4 panel that i switch then in wizard style ( asp 1.1) . i want to know if i can hide/show the asp panel control IN THE CLIENT SIDE ? meaning, show panel 1 . user enter...
2
by: Mel | last post by:
I have a table with two TDs (LHS & RHS), each TD contains a frame full of stuff. on one of the frames (RHS) I have an icon with onClick attached to show/hide LHS. My code goes like this: ...
11
by: dmorand | last post by:
I'm having some trouble with my javascript which is supposed to hide/show a div element. I have to click on the link twice before it'll hide. I can't seem to figure out why the first click does...
0
by: Charles Fox | last post by:
Hi guys, I'm playing with Python in emacs, with python mode. I'd like to be able to press a key to toggle the code comments on and off -- to switch between beautiful clean Python code, and the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.