wxPython 菜单项,菜单和MenuBar
顶级窗口标题栏正下方的水平栏保留显示一系列菜单。它是wxPython API 中 wx.MenuBar类 的对象。
wx.Menu类的对象被添加到菜单栏中。它还用于创建上下文菜单和弹出菜单。每个菜单可能包含一个或多个wx.MenuItem对象或级联的Menu对象。
wx.MenuBar类除了默认构造函数之外还有一个参数化构造函数。
wx.MenuBar() wx.MenuBar(n, menus, titles, style)
参数'n'表示菜单的数量。 Menu 是一个菜单和标题数组,以及一个字符串数组。如果style参数设置为wx.MB_DOCKABLE,则可以停靠菜单栏。
以下是wx.MenuBar类的方法列表:
| 序号 | 方法和描述 |
|---|---|
| 1 |
Append() 将菜单对象添加到栏 |
| 2 |
Check() 检查或取消选中菜单 |
| 3 |
Enable() 启用或禁用菜单 |
| 4 |
Remove() 从栏中删除菜单 |
wx.Menu类对象是一个或多个菜单项的下拉列表,其中一个菜单项可以由用户选择。
下表显示了wx.Menu类经常需要的方法:
| 序号 | 方法和描述 |
|---|---|
| 1 |
附加() 在菜单中添加菜单项 |
| 2 |
AppendMenu() 附加子菜单 |
| 3 |
AppendRadioItem() 附加可选择的广播项目 |
| 4 |
AppendCheckItem() 附加可检查的菜单项 |
| 5 |
AppendSeparator() 添加分隔线 |
| 6 |
Insert( ) 在给定位置插入新菜单 |
| 7 |
InsertRadioItem() 在给定位置插入电台项目 |
| 8 |
InsertCheckItem() 在给定位置插入新的检查项目 |
| 9 |
InsertSeparator() 插入分隔线 |
| 10 |
Remove( ) 从菜单中删除项目 |
| 11 |
GetMenuItems() 返回菜单项列表 |
甲 菜单项 可使用追加()函数被直接加入,或wx.MenuItem类的一个对象被用于追加。
wx.Menu.Append(id, text, kind) Item = Wx.MenuItem(parentmenu, id, text, kind) wx.Menu.Append(Item)
为了定义菜单项,必须提到要添加它的菜单。
wxPython有大量标准ID分配给标准菜单项。在某些操作系统平台上,它们也与标准图标相关联。
| wx.ID_SEPARATOR |
| wx.ID_ANY |
| wx.ID_OPEN |
| wx.ID_CLOSE |
| wx.ID_NEW |
| wx.ID_SAVE |
| wx.ID_SAVEAS |
| wx.ID_EDIT |
| wx.ID_CUT |
| wx.ID_COPY |
| wx.ID_PASTE |
但是,任何唯一的整数都可以指定为ID。text参数是其标题。Kind参数采用以下枚举器:
| 序号 | 参数和描述 |
|---|---|
| 1 |
wx.ITEM_NORMAL 普通菜单项 |
| 2 |
wx.ITEM_CHECK 检查(或切换)菜单项 |
| 3 |
wx.ITEM_RADIO 收音机菜单项 |
wx.Menu类还有AppendRadioItem()和AppendCheckItem(),它们不需要kind参数。
菜单项可以设置为显示图标或快捷方式。wx.MenuItem类的SetBitmap()函数需要显示位图对象。
wx.MenuItem.SetBitmap(wx.Bitmap(image file))
EVT_MENU事件活页夹有助于处理菜单选择。
self.Bind(wx.EVT_MENU, self.menuhandler)
范例
以下示例演示了wxPython中菜单系统的大部分上述功能。它显示菜单栏中显示的文件菜单。普通菜单项,子菜单,广播项和检查项都会添加到其中。还存在具有图标的菜单项。
调用时,事件处理程序检索与事件关联的ID,并可进一步处理。例如,如果选择“新建”菜单项,则会在框架的文本框中回显它。
完整的代码如下:
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title, size = (250,150))
self.InitUI()
def InitUI(self):
menubar = wx.MenuBar()
fileMenu = wx.Menu()
newitem = wx.MenuItem(fileMenu,wx.ID_NEW, text = "New",kind = wx.ITEM_NORMAL)
newitem.SetBitmap(wx.Bitmap("new.bmp"))
fileMenu.AppendItem(newitem)
fileMenu.AppendSeparator()
editMenu = wx.Menu()
copyItem = wx.MenuItem(editMenu, 100,text = "copy",kind = wx.ITEM_NORMAL)
copyItem.SetBitmap(wx.Bitmap("copy.bmp"))
editMenu.AppendItem(copyItem)
cutItem = wx.MenuItem(editMenu, 101,text = "cut",kind = wx.ITEM_NORMAL)
cutItem.SetBitmap(wx.Bitmap("cut.bmp"))
editMenu.AppendItem(cutItem)
pasteItem = wx.MenuItem(editMenu, 102,text = "paste",kind = wx.ITEM_NORMAL)
pasteItem.SetBitmap(wx.Bitmap("paste.bmp"))
editMenu.AppendItem(pasteItem)
fileMenu.AppendMenu(wx.ID_ANY, "Edit", editMenu)
fileMenu.AppendSeparator()
radio1 = wx.MenuItem(fileMenu, 200,text = "Radio1",kind = wx.ITEM_RADIO)
radio2 = wx.MenuItem(fileMenu, 300,text = "radio2",kind = wx.ITEM_RADIO)
fileMenu.AppendItem(radio1)
fileMenu.AppendItem(radio2)
fileMenu.AppendSeparator()
fileMenu.AppendCheckItem(103,"Checkable")
quit = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+Q')
fileMenu.AppendItem(quit)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
self.text = wx.TextCtrl(self,-1, style = wx.EXPAND|wx.TE_MULTILINE)
self.Bind(wx.EVT_MENU, self.menuhandler)
self.SetSize((350, 250))
self.Centre()
self.Show(True)
def menuhandler(self, event):
id = event.GetId()
if id == wx.ID_NEW:
self.text.AppendText("new"+"\n")
ex = wx.App()
Mywin(None,'MenuBar demo')
ex.MainLoop()
上面的代码产生以下输出:

下一章:wxPython ToolBar类
包括具有文本标题或图标的按钮的一个或多个水平条带工具栏通常位于顶层框架中的MenuBar的正下方。如果 wx.Toolbar 对象的style参数设置为wx.TB_DOCKABLE,则它变为可停靠。也可以 ...
AI 中文社