A common part in a GUI application is a menubar. A menubar consists of objects called menus. Top-level menus have their labels on the menubar. The menus have menu items. Menu items are commands that perform a specific action inside the application. Menus can also have submenus, which have their own menu items. The following three classes are used to create menubars in wxPython. A wx.MenuBar, a wx.Menu and a wx.MenuItem.
一个菜单包括菜单的对象。顶层菜单有菜单栏上的标签。菜单菜单项。菜单项命令执行一个应用程序内的具体行动。菜单中还可以有子菜单,其中有自己的菜单项。以下三个类用于创建在wxPython的菜单栏。一个wx.MenuBar,wx.Menu和wx.MenuItem。
Simple menu
简单的菜单
In our first example, we will create a menubar with one file menu. The menu will have only one menu item. By selecting the item the application quits.在我们的第一个例子中,我们将创建一个menubar,一个文件菜单。菜单将只有一个菜单项。通过选择项应用程序退出。
import wx class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): menuBar = wx.MenuBar() filemenu = wx.Menu() fitem = filemenu.Append(wx.ID_EXIT,"Quit","Quit Applications") menuBar.Append(filemenu,"&File") self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.OnQuit, fitem) self.SetSize((400,250)) self.SetTitle("SimpleMenu") #self.Centre() self.Center() self.Show() def OnQuit(self,e): self.Close() def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
menubar = wx.MenuBar()首先我们创建一个menubar对象。
fileMenu = wx.Menu()接下来,我们创建一个菜单对象。
fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
我们追加到菜单项的菜单对象。第一个参数是菜单项的ID。标准ID会自动添加一个图标和快捷。 CTRL + Q在我们的例子。第二个参数是菜单项的名称。最后一个参数定义状态栏上显示的菜单项被选中时,简短的帮助字符串。在这里,我们没有创造出wx.MenuItem明确。它是幕后的append()方法创建。该方法返回创建的菜单项。此参考将使用后绑定事件。
self.Bind(wx.EVT_MENU, self.OnQuit, fitem)
我们绑定菜单项wx.EVT_MENU的的的定制OnQuit()方法。这种方法将关闭应用程序。
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
之后,我们追加到菜单栏菜单。 &字符创建一个快捷键。后面的字符下划线。这种方式是通过按Alt + F快捷访问菜单。最后,我们呼吁的SetMenuBar()方法。这种方法属于wx.Frame的部件。它设置的菜单栏。
Icons and shortcuts图标与快捷键
The next example is esencially the same as the previous one. This time, we manually create a wx.MenuItem.
在下一个例子基本上是与前一个相同。这一次,我们手动创建一个wx.MenuItem。
import wx APP_EXIT = 1 class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): menuBar = wx.MenuBar() filemenu = wx.Menu() qmi = wx.MenuItem(filemenu,APP_EXIT,"&Quit\tCtrl+Q") qmi.SetBitmap(wx.Bitmap("exit.png")) filemenu.AppendItem(qmi) self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT) menuBar.Append(filemenu, '&File') self.SetMenuBar(menuBar) self.SetSize((250, 200)) self.SetTitle('Icons and shortcuts') self.Centre() self.Show(True) def OnQuit(self, e): self.Close() def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)
当我们选择创建菜单项,OnQuit()方法将被调用。
Submenus and separators子菜单和分隔符
Each menu can also have a submenu. This way we can place similar commands into groups. For example we can place commands that hide/show various toolbars like personal bar, address bar, status bar or navigation bar into a submenu called toolbars. Within a menu, we can seperate commands with a separator. It is a simple line. It is common practice to separate commands like New, Open, Save from commands like Print, Pri
每个菜单,也可以有一个子菜单。这样我们就可以把成组类似的命令。例如,我们可以将像个人栏,地址栏,状态栏或导航栏,将工具栏子菜单隐藏/显示各种工具栏的命令。在菜单中,我们可以逗号分开一个分隔的命令。这是一个简单的线条。常见的做法是单独的命令,如新建,打开,保存,如打印,打印预览命令与一个单一的分离。在我们的例子中,我们将看到,我们如何能够创建子菜单和菜单分隔。
''' Created on 2012-6-30 @author: Administrator ''' import wx class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): menuBar = wx.MenuBar() fileMenu = wx.Menu() fileMenu.Append(wx.ID_NEW,"&New") fileMenu.Append(wx.ID_OPEN, '&Open') fileMenu.Append(wx.ID_SAVE, '&Save') fileMenu.AppendSeparator() imp = wx.Menu() imp.Append(wx.ID_ANY, 'Import newsfeed list...') imp.Append(wx.ID_ANY, 'Import bookmarks...') imp.Append(wx.ID_ANY, 'Import mail...') qmi = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W') fileMenu.AppendItem(qmi) menuBar.Append(fileMenu, '&File') self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.OnQuit, qmi) self.SetSize((400,250)) self.SetTitle("SimpleMenu") #self.Centre() self.Center() self.Show() def OnQuit(self,e): self.Close() def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
fileMenu.Append(wx.ID_NEW, '&New') fileMenu.Append(wx.ID_OPEN, '&Open') fileMenu.Append(wx.ID_SAVE, '&Save')
fileMenu.AppendSeparator()
imp = wx.Menu() imp.Append(wx.ID_ANY, 'Import newsfeed list...') imp.Append(wx.ID_ANY, 'Import bookmarks...') imp.Append(wx.ID_ANY, 'Import mail...') fileMenu.AppendMenu(wx.ID_ANY, 'I&mport', imp)
Check menu item复选菜单
There are tree kinds of menu items. 它们有三种
normal item
check item
radio item
In the following example, we will demonstrate the check menu item. A check menu item is visually represented by a tick in the menu.
在接下来的例子中,我们将演示如何检查菜单项。一个检查菜单项是视觉上表示为一个滴答在菜单。
''' Created on 2012-6-30 @author: Administrator ''' import wx class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): menuBar = wx.MenuBar() filemenu = wx.Menu() viewmenu = wx.Menu() self.shst = viewmenu.Append(wx.ID_ANY,"ShowStatubar","ShowStatubar",kind=wx.ITEM_CHECK) self.shtl = viewmenu.Append(wx.ID_ANY,"ShowToolBar","ShowToolBar",kind=wx.ITEM_CHECK) viewmenu.Check(self.shst.GetId(),True) viewmenu.Check(self.shtl.GetId(),True) self.Bind(wx.EVT_MENU, self.ToggleStatuBar, self.shst) self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl) menuBar.Append(filemenu, '&File') menuBar.Append(viewmenu, '&View') self.SetMenuBar(menuBar) self.toolbar = self.CreateToolBar() self.toolbar.AddLabelTool(1,'',wx.Bitmap("exit.png")) self.toolbar.Realize() self.statusbar = self.CreateStatusBar() self.statusbar.SetStatusText('Ready') self.SetSize((350, 250)) self.SetTitle('Check menu item') self.Centre() self.Show(True) def ToggleStatuBar(self,e): if self.shst.IsChecked(): self.statusbar.Show() else: self.statusbar.Hide() def ToggleToolBar(self, e): if self.shtl.IsChecked(): self.toolbar.Show() else: self.toolbar.Hide() def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
self.shst = viewMenu.Append(wx.ID_ANY, 'Show statubar', 'Show Statusbar', kind=wx.ITEM_CHECK) self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar', 'Show Toolbar', kind=wx.ITEM_CHECK)
viewMenu.Check(self.shst.GetId(), True) viewMenu.Check(self.shtl.GetId(), True)
def ToggleStatusBar(self, e): if self.shst.IsChecked(): self.statusbar.Show() else: self.statusbar.Hide()
Context menu右键菜单
A context menu is a list of commands that appears under some context. For example, in a Firefox web browser, when we right click on a web page, we get a context menu. Here we can reload a page, go back or view page source. If we right click on a toolbar, we get another context menu for managing toolbars. Context menus are sometimes called popup menus.
上下文菜单在某些情况下出现的命令的列表。例如,在Firefox网页浏览器,当我们在网页上右击,我们得到一个上下文菜单。在这里,我们可以重新载入页面,回去或查看页面的源代码。如果我们右键单击工具栏上,我们得到另一个管理工具栏的上下文菜单。有时也被称为上下文菜单弹出菜单。
''' Created on 2012-6-30 @author: Administrator ''' import wx class MyPopupMenu(wx.Menu): def __init__(self,parent): super(MyPopupMenu,self).__init__() self.parent = parent mmi = wx.MenuItem(self,wx.NewId(),'MiniSize') self.AppendItem(mmi) self.Bind(wx.EVT_MENU, self.OnMinimize, mmi) cmi = wx.MenuItem(self,wx.NewId(),'Close') self.AppendItem(cmi) self.Bind(wx.EVT_MENU, self.OnClose, cmi) def OnMinimize(self,e): self.parent.Iconize() def OnClose(self,e): self.parent.Close() class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown) self.SetSize((250, 200)) self.SetTitle('Context menu') self.Centre() self.Show(True) def OnRightDown(self,e): self.PopupMenu(MyPopupMenu(self),e.GetPosition()) def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
class MyPopupMenu(wx.Menu): def __init__(self, parent): super(MyPopupMenu, self).__init__()
mmi = wx.MenuItem(self, wx.NewId(), 'Minimize') self.AppendItem(mmi) self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
def OnRightDown(self, e): self.PopupMenu(MyPopupMenu(self), e.GetPosition())
Toolbars工具条
Menus group all commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands.
菜单组的所有命令,我们可以在应用程序中使用。工具栏提供一个快速访问最常用的命令。
To create a toolbar, we call the CreateToolBar() method of the frame widget.
要创建一个工具栏,我们调用CreateToolBar()方法的框架部件。
import wx class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): toolbar = self.CreateToolBar() qtool = toolbar.AddLabelTool(wx.ID_ANY,"Quit",wx.Bitmap("exit.png")) toolbar.Realize() self.Bind(wx.EVT_TOOL, self.OnQuit, qtool) self.SetSize((500,300)) self.Centre() self.Show(True) def OnQuit(self,e): self.Close() def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
toolbar = self.CreateToolBar()
qtool = toolbar.AddLabelTool(wx.ID_ANY, 'Quit', wx.Bitmap('texit.png'))
toolbar.Realize()
如果我们想要创建一个以上的工具栏,我们必须采取不同的方式。
import wx class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): vbox = wx.BoxSizer(wx.VERTICAL) toolbar1 = wx.ToolBar(self) toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap("1.png")) toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap("2.png")) toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap("3.png")) toolbar1.Realize() toolbar2 = wx.ToolBar(self) qtool = toolbar2.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap("exit.png")) toolbar2.Realize() vbox.Add(toolbar1,0,wx.EXPAND) vbox.Add(toolbar2,0,wx.EXPAND) self.Bind(wx.EVT_TOOL, self.OnQuit, qtool) self.SetSizer(vbox) self.SetSize((500,300)) self.Centre() self.Show(True) def OnQuit(self,e): self.Close() def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
toolbar1 = wx.ToolBar(self) ... toolbar2 = wx.ToolBar(self)
Enable, disable启用、禁用
In the following example, we will show, how we can enable and disable toolbar buttons. We will also see a separator line.
在接下来的例子中,我们将展示,我们如何启用和禁用的工具栏按钮。我们还将看到一条分隔线。
''' Created on 2012-7-1 @author: Administrator ''' import wx class Example(wx.Frame): def __init__(self,*args,**kw): super(Example,self).__init__(*args,**kw) self.InitUI() def InitUI(self): self.count = 5 self.toolbar = self.CreateToolBar() tundo = self.toolbar.AddLabelTool(wx.ID_UNDO,'',wx.Bitmap("1.png")) tredo = self.toolbar.AddLabelTool(wx.ID_REDO,'',wx.Bitmap("2.png")) self.toolbar.EnableTool(wx.ID_REDO,False) self.toolbar.AddSeparator() texit = self.toolbar.AddLabelTool(wx.ID_EXIT,'',wx.Bitmap("exit.png")) self.toolbar.Realize() self.Bind(wx.EVT_TOOL, self.OnQuit,texit) self.Bind(wx.EVT_TOOL, self.OnUndo,tundo) self.Bind(wx.EVT_TOOL, self.OnTredo,tredo) self.SetSize((500,300)) self.Centre() self.Show(True) def OnQuit(self,e): self.Close() def OnUndo(self,e): 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) def OnTredo(self,e): if self.count < 5 and self.count >= 1: self.count = self.count + 1 if self.count == 5: self.toolbar.EnableTool(wx.ID_REDO,False) if self.count == 2: self.toolbar.EnableTool(wx.ID_UNDO,True) def main(): ex = wx.App() Example(None) ex.MainLoop() if __name__ == '__main__': main()
self.toolbar.EnableTool(wx.ID_REDO, False) self.toolbar.AddSeparator()
def OnUndo(self, e): 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)