這兩天一直在學習菜單工具欄之類的問題,上午正好有機會和你們討論一下.java
Package wx :: Class Menu Type Menu Method Summary Menu __init__(self, title, style) __repr__(self) MenuItem Append(self, id, text, help, kind) MenuItem AppendCheckItem(self, id, text, help) MenuItem AppendItem(self, item) MenuItem AppendMenu(self, id, text, submenu, help) Package wx :: Class MenuBar Type MenuBar Window __init__(self, parent, id, pos, size, style, name) Construct and show a generic Window. __repr__(self) bool Append(self, menu, title) Attach(self, frame) Check(self, id, check) Detach(self) bool Enable(self, enable) Enable or disable the window for user input. EnableTop(self, pos, enable) Package wx :: Class EvtHandler Type EvtHandler EvtHandler __init__(self) __repr__(self) AddPendingEvent(self, event) Bind(self, event, handler, source, id, id2)
在咱們的第一個例子中,咱們將創立一個menubar,一個文件菜單。菜單將只有一個菜單項。經過選擇項應用程序退出。程序員
示例代碼以下:編程
import wx class Example(wx.Frame): def __init__(self,*args,**kw): wx.Frame.__init__(self,None) def InitUI(self): menuBar = wx.MenuBar() filemenu = wx.Menu() fitem = filemenu.Append(001,"exit","Quit Applications") fitem1 = filemenu.Append(002,"002","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.Center() self.Show() def OnQuit(self,e): self.Close() def main(): app = wx.App() exa = Example(None) exa.InitUI() app.MainLoop() if __name__ == '__main__': main()
這是一個以最小的菜單功能小例子。
menubar = wx.MenuBar()首先咱們創立一個menubar對象。
fileMenu = wx.Menu()接下來,咱們創立一個菜單對象。
fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
咱們追加到菜單項的菜單對象。第一個參數是菜單項的ID。標準ID會自動添加一個圖標和快捷(在windows上沒有出現圖標,也不能用快捷鍵)。 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快捷拜訪菜單(windows上彷佛不能用)。最後,咱們吶喊的SetMenuBar()方法。這類方法屬於wx.Frame的部件。它設置的菜單欄。windows
運行示例圖api
在這個例子中,會手動的創立一個快捷鍵與圖標,以下圖所示:瀏覽器
示例代碼以下:app
import wx APP_EXIT = 1 class Example(wx.Frame): def __init__(self,*args,**kw): wx.Frame.__init__(self,None) def InitUI(self): menuBar = wx.MenuBar() filemenu = wx.Menu() qmi = wx.MenuItem(filemenu,APP_EXIT,"&Quit\tCtrl+Q") bitm = wx.Bitmap("my.png") bitm.SetSize(size=(20,20)) qmi.SetBitmap(bitm) 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(): app = wx.App() exa = Example(None) exa.InitUI() app.MainLoop() if __name__ == '__main__': main()
每一個菜單,也能夠有一個子菜單。這樣咱們就能夠把成組相似的命令。例如,咱們能夠將像我的欄,地址欄,狀態欄或導航欄,將工具欄子菜單隱藏/顯示各類工具欄的命令。在菜單中,咱們能夠逗號離開一個離開的命令。這是一個簡單的線條。常見的作法是單獨的命令,如新建,打開,保存,如打印,打印預覽命令與一個單一的分離。在咱們的例子中,咱們將看到,咱們如何能夠創立子菜單和菜單離開ide
示例圖以下:工具
示例代碼:oop
import wx class Example(wx.Frame): def __init__(self,*args,**kw): wx.Frame.__init__(self,None) 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.AppendMenu(wx.ID_ANY, 'I&mport', imp) 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(): app = wx.App() ex = Example(None) ex.InitUI() app.MainLoop() if __name__ == '__main__': main()
There are tree kinds of menu items. 它們有三種
normal item
check item
radio item
在接下來的例子中,咱們將演示如何檢查菜單項。一個檢查菜單項是視覺上表現爲一個滴答在菜單。
效果圖示:
示例代碼:
import wx class Example(wx.Frame): def __init__(self,*args,**kw): wx.Frame.__init__(self,None); 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() bitm = wx.Bitmap("my.png") bitm.SetSize(size=(20,20)) self.toolbar.AddLabelTool(1,'',bitm) 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(): app = wx.App() exa = Example(None) exa.InitUI() app.MainLoop() if __name__ == '__main__': main()
上下文菜單在某些狀況下出現的命令的列表。例如,在Firefox網頁瀏覽器,當咱們在網頁上右擊,咱們失掉一個上下文菜單。在這裏,咱們能夠重新載入頁面,回去或查看頁面的源代碼。若是咱們右鍵單擊工具欄上,咱們失掉另外一個管理工具欄的上下文菜單。偶然也被稱爲上下文菜單彈出菜單。
運行效果圖:
示例代碼:
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): wx.Frame.__init__(self,None); 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(): app = wx.App() exa = Example(None) exa.InitUI() app.MainLoop() if __name__ == '__main__': main()
參考:
wxPython中文教程 簡單入門加實例
如何重新設置Bitmap的鉅細?http://social.msdn.microsoft.com/Forums/pt-BR/ce4e0b9e-549f-4fb7-a107-a1c3a37839dc/bitmap
文章結束給你們分享下程序員的一些笑話語錄: 一條狗在街上閒逛,看見櫥窗裏一張告示:「招聘程序員。會編程,有團隊精神,至少精通兩種語言。均等機會。」
那條狗就進去申請,可是被拒絕了。
「我不能僱一條狗在公司裏作事。」經理說。
狗不服氣,指着告示上「均等機會」幾字抗議。
經理無法,嘆了口氣,不屑地問道:「你會編程嗎?」
那條狗默默地走到電腦前,編了個程序,運做準確。
「你有團隊精神嗎?」經理問。
那條狗掉頭看了看門外,一大羣野狗在外面虎視耽耽。
「我真的不能僱狗作這份工做。」經理氣急敗壞地說。
「就算會編程、有團隊精神,可是我須要的僱員至少要能精通兩種語言。」
那條狗擡頭看着經理說:「喵-噢。」
--------------------------------- 原創文章 By 菜單和工具欄---------------------------------