wxPython的Controls组件学习1----Menus and Toolbars

To implement a menubar in wxPython we need to have three things. A wx.MenuBar, a wx.Menu and a wx.MenuItem.


1、创建一个MenuBar对象

menubar = wx.MenuBar()

 

2、创建一个菜单

file = wx.Menu()
file.Append(-1, 'Quit', 'Quit application')

 

The first parameter is the id of the menu item.

The second parameter is the name of the menu item.

The last parameter defines the short helpstring that is displayed on the statusbar

 

menubar.Append(file, '&File')
self.SetMenuBar(menubar)

 

3、添加MenuItem元素:

注意:

MenuItem Append(self, id, text, help, kind) 
MenuItem AppendCheckItem(self, id, text, help) 
MenuItem AppendItem(self, item) 
MenuItem AppendMenu(self, id, text, submenu, help) 
MenuItem AppendRadioItem(self, id, text, help) 
MenuItem AppendSeparator(self) 

 

Menu的append方法返回的都是MenuItem类型的,这就说明Menu是一个菜单列表,声明一个MenuItem,作为Menu的一个元素。

 

 Append方法只是添加了一个普通的MenuItem元素,而AppendItem则是这样的:

 menubar = wx.MenuBar()
 file = wx.Menu()
 #为菜单设置小图标
 quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
 quit.SetBitmap(wx.Bitmap('icons/exit.png'))
 file.AppendItem(quit)

 

下面这段话是对AppendItem的一个说明:

If we want to add shortcuts and icons to our menus, we have to manually create a wx.MenuItem. So far we have created menuitems indirectly. The & character specifies an accelerator key. The following character is underlined. The actual shortcut is defined by the combination of characters. We have specified Ctrl + Q characters. So if we press Ctrl + Q, we close the application. We put a tab character between the & character and the shortcut. This way, we manage to put some space between them. To provide an icon for a menuitem, we call a SetBitmap() method. A manually created menuitem is appended to the menu by calling the AppendItem() method.

 

可以这样理解:

一个MenuBar可以放置多个Menu,一个Menu可以放置多个MenuItem作为其元素。
 

self.Bind(wx.EVT_MENU, self.OnQuit, Source)

第三个参数Source就是指定一个MenuItem本身,当然也可以通过id来制定一个元素。

 

 

4、子菜单:

file.AppendSeparator()

 
A menu separator is appended with the AppendSeparator() method.

 

 imp = wx.Menu()
 imp.Append(-1, 'Import newsfeed list...')
 imp.Append(-1, 'Import bookmarks...')
 imp.Append(-1, 'Import mail...')

 file.AppendMenu(-1, 'I&mport', imp)

 

Creating a submenu is trivial.
First, we create a menu.
Then we append menu items.
A submenu is created by calling the AppenMenu() on the menu object.

 

5、其他样式的MenuItem

normal item
check item
radio item

 

6、Context menu------单击鼠标右键而产生的环境菜单

 

 

 

2>工具栏:

1、创建工具栏:
ToolBar一般利用wx.Frame的CreateToolBar方法来创建
toolbar = self.CreateToolBar()
toolbar.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
toolbar.Realize()
After we have put our items to the toolbar, we call the Realize() method. Calling this method is not obligatory on Linux. On windows it is.

 

2、If we want to create more than one toolbars, we must do it differently.

        vbox = wx.BoxSizer(wx.VERTICAL)

        toolbar1 = wx.ToolBar(self, -1)
        toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/new.png'))
        toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/open.png'))
        toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/save.png'))
        toolbar1.Realize()

        toolbar2 = wx.ToolBar(self, -1)
        toolbar2.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('../icons/exit.png'))
        toolbar2.Realize()

        vbox.Add(toolbar1, 0, wx.EXPAND)
        vbox.Add(toolbar2, 0, wx.EXPAND)

        self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)

        self.SetSizer(vbox)

 

3、Sometimes we need to create a vertical toolbar. Vertical toolbars are often seen in graphics applications like Inkscape or Xara Xtreme.

 toolbar = self.CreateToolBar(wx.TB_VERTICAL)
 toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/select.gif'))
 toolbar.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('../icons/freehand.gif'))
 ...

 

4、In the following example, we will show, how we can enable and disable toolbar buttons. We will also see a separator line.

In our example, we have three toolbar buttons. One button is for exiting the application. The other two buttons are undo and redo buttons. They simulate undo/redo functionality in an application. (for a real example, see tips and tricks) We have 4 changes. The undo and redo butons are disabled accordingly.  

 self.toolbar.EnableTool(wx.ID_REDO, False)
 self.toolbar.AddSeparator()

In the beginning, the redo button is disabled. We do it by calling the EnableTool() method. We can create some logical groups within a toolbar. We can separate various groups of buttons by a small vertical line. To do this, we call the AddSeparator() method.

 

 def OnUndo(self, event):
     if self.count > 1 and self.count <= 5:
         self.count = self.count - 1

     if self.count == 1:
         self.toolbar.EnableTool(wx.ID_UNDO, False)

     if self.count == 4:
         self.toolbar.EnableTool(wx.ID_REDO, True)

 

We simulate undo and redo functionality. We have 4 changes. If there is nothing left to undo, the undo button is disabled. After undoing the first change, we enable the redo button. Same logic applies for the OnRedo() method.

 

 

你可能感兴趣的:(linux,windows,wxPython)