wxPython中工具欄、狀態欄、菜單實現

   wxPython是python可視化編程中的一個很好的模塊,一如下的代碼主要講述工具欄、狀態欄、菜單、菜單事件的實現(可參考:http://www.czug.org/python/wxpythoninaction/):python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.py.p_w_picpaths
class ToolbarFrame(wx.Frame):
    def __init__(self, parent, id):
          
        wx.Frame.__init__(self, parent, id, 'Toolbars', size = (600, 400))
          
        panel = wx.Panel(self)
        panel.SetBackgroundColour('White')
          
        #建立狀態欄
        statusBar = self.CreateStatusBar()
          
        #建立工具欄
        toolbar = self.CreateToolBar()
        #增長一個工具
        toolbar.AddSimpleTool(wx.NewId(), wx.py.p_w_picpaths.getPyBitmap(), "New", "Long help for 'New'")
        toolbar.AddSimpleTool(wx.NewId(), wx.py.p_w_picpaths.getPyBitmap(), "Edit", "Long help for 'Edit'")
        #準備顯示
        toolbar.Realize()
          
        #建立菜單
        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        menuBar.Append(menu1, u"&文件") #菜單項目1
        self.close = menu1.Append(wx.NewId(), u"退出(&X)", "")
        menu2 = wx.Menu()
        #菜單內容&表示隨後的字符爲熱鍵,參數3爲在狀態欄上顯示的菜單項說明
        self.Copy = menu2.Append(wx.NewId(), "&Copy", "Copy in status bar")
        self.Cut = menu2.Append(wx.NewId(), "C&ut", "")
        self.Paste = menu2.Append(wx.NewId(), "Paste", "")
        menu2.AppendSeparator()
        self.Options = menu2.Append(wx.NewId(), "&Options...", "Display Options")
        self.Edit = menuBar.Append(menu2, "&Edit")
        self.SetMenuBar(menuBar)
        #調用菜單下拉的退出事件
        self.Bind(wx.EVT_MENU,self.OnClose,self.close)
    def OnClose(self,event):#退出事件
        self.Close()
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = ToolbarFrame(parent = None, id = -1)
    frame.Show()
    app.MainLoop()
相關文章
相關標籤/搜索