wxPython菜單與工具列基礎樣本

來源:互聯網
上載者:User
文章目錄
  • 1.基本的api介紹
  • 2.簡單菜單
  • 3.快速鍵與表徵圖
  • 4.子功能表和分隔字元
  • 5.複選菜單Check menu item
  • 6.右鍵菜單Context menu

測試環境:

系統;windows xp

python version: 2.7.4

1.基本的api介紹

Package wx :: Class Menu Type MenuMethod 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 MenuBarWindow      __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 EvtHandlerEvtHandler   __init__(self)                       __repr__(self)                      AddPendingEvent(self, event)                      Bind(self, event, handler, source, id, id2)  
2.簡單菜單

在我們的第一個例子中,我們將建立一個menubar,一個檔案菜單。菜單將只有一個功能表項目。通過選擇項應用程式退出。

範例程式碼如下:

import wxclass 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的組件。它設定的功能表列。

運行樣本圖

3.快速鍵與表徵圖

在這個例子中,會手動的建立一個快速鍵與表徵圖,如所示:

範例程式碼如下:

import wxAPP_EXIT = 1class 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()

4.子功能表和分隔字元

每個菜單,也可以有一個子功能表。這樣我們就可以把成組類似的命令。例如,我們可以將像個人欄,地址欄,狀態列或導覽列,將工具列子功能表隱藏/顯示各種工具列的命令。在菜單中,我們可以逗號分開一個分隔的命令。這是一個簡單的線條。常見的做法是單獨的命令,如建立,開啟,儲存,如列印,預覽列印命令與一個單一的分離。在我們的例子中,我們將看到,我們如何能夠建立子功能表和菜單分隔

樣本圖如下:

範例程式碼:

import wxclass 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()

5.複選菜單Check menu item

There are tree kinds of menu items. 它們有三種
normal item
check item
radio item

在接下來的例子中,我們將示範如何檢查功能表項目。一個檢查功能表項目是視覺上表示為一個滴答在菜單。

示:

範例程式碼:

import wxclass 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()

6.右鍵菜單Context menu

操作功能表在某些情況下出現的命令的列表。例如,在Firefox網頁瀏覽器,當我們在網頁上右擊,我們得到一個操作功能表。在這裡,我們可以重新載入頁面,回去或查看頁面的原始碼。如果我們按右鍵工具列上,我們得到另一個管理工具列的操作功能表。有時也被稱為操作功能表快顯功能表。

運行:

範例程式碼:

import wxclass 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中文教程 簡單入門加執行個體

wxPython菜單與工具列

如何重新設定Bitmap的大小?http://social.msdn.microsoft.com/Forums/pt-BR/ce4e0b9e-549f-4fb7-a107-a1c3a37839dc/bitmap

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.