python中初学者隐藏显示工具栏的一般帮助

嗨伙计们!
我有下面的代码,一切都在工作,除了我不能让隐藏工具栏的函数工作。最初我使用的是与教程不同的方法,它是有效的,但现在我添加了新的东西并更改了一些旧的东西,我遇到了一个问题。

选择 | 换行 | 行号
  1. error: AttributeError: 'Example' object has no attribute 'toolbar'
  2.  

我尝试使用"TOOLBAR=TOOLBAR.Hide()"和"TOOLBAR=HIDE()"以及其他一些方法来更改函数"TOOLBAR.Hide()",但不起作用。谁能给我解释一下这两种方法之间的区别以及如何解决这个问题?
此外,一个好的Python函数引用将是很好的。
提前谢谢!
代码的重要部分,新的appraoch:

选择 | 换行 | 行号
  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.  

代码旧方法的重要部分。

选择 | 换行 | 行号
  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.  

完整代码,新方法:

选择 | 换行 | 行号
  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.  

标签: python

添加新评论