wx.Menu類的一個對象被添加到菜單欄。它也用於創建上下文菜單和彈出菜單。每個菜單可以包含一個或多個wx.MenuItem對象或級聯Menu對象。
wx.MenuBar() wx.MenuBar(n, menus, titles, style)
參數“n”表示的菜單的數目。Menu是菜單和標題的數組和字串數組。如果style參數設置為wx.MB_DOCKABLE,菜單欄可以停靠。
| S.N. |
方法和說明
|
|---|---|
| 1 |
Append()
添加菜單對象到工具欄
|
| 2 |
Check()
選中或取消選中菜單
|
| 3 |
Enable()
啟用或禁用菜單
|
| 4 |
Remove()
去除工具欄中的菜單
|
| S.N. |
方法和說明
|
|---|---|
| 1 |
Append()
在菜單增加了一個菜單項
|
| 2 |
AppendMenu()
追加一個子菜單
|
| 3 |
AppendRadioItem()
追加可選當選項
|
| 4 |
AppendCheckItem()
追加一個可檢查的菜單項
|
| 5 |
AppendSeparator()
添加一個分隔線
|
| 6 |
Insert()
在給定的位置插入一個新的菜單
|
| 7 |
InsertRadioItem()
在給定位置插入單選項
|
| 8 |
InsertCheckItem()
在給定位置插入新的檢查項
|
| 9 |
InsertSeparator()
插入分隔行
|
| 10 |
Remove()
從菜單中刪除一個項
|
| 11 |
GetMenuItems()
返回菜單項列表
|
wx.Menu.Append(id, text, kind) Item = Wx.MenuItem(parentmenu, id, text, kind) wx.Menu.Append(Item)
| 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 |
| S.N. | 參數 & 描述 |
|---|---|
| 1 |
wx.ITEM_NORMAL
普通菜單項
|
| 2 |
wx.ITEM_CHECK
檢查(或切換)菜單項
|
| 3 |
wx.ITEM_RADIO
單選菜單項
|
wx.MenuItem.SetBitmap(wx.Bitmap(image file))
self.Bind(wx.EVT_MENU, self.menuhandler)
實例
下麵的例子演示了wxPython的上述大部分的菜單系統的特徵。它顯示在菜單欄中顯示一個檔菜單。普通菜單項,子菜單,單選項和檢查項加入。菜單項也帶有一個圖示顯示。
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title, size = (400,300))
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 - xuhuhu.com')
ex.MainLoop()
上面的代碼產生下麵的輸出 -
