python之wxPython菜单使用详解


Posted in Python onSeptember 28, 2014

本文实例讲述了python中wxPython菜单的使用方法,分享给大家供大家参考。具体如下:

先来看看下面这段代码:

import wx 
APP_EXIT=1  #定义一个控件ID 
 
class Example(wx.Frame): 
  def __init__(self, parent, id, title): 
    super(Example,self).__init__(parent, id, title)    #调用你类的初始化 
 
    self.InitUI()      #调用自身的函数 
 
  def InitUI(self):  #自定义的函数,完成菜单的设置 
 
    menubar = wx.MenuBar()    #生成菜单栏 
    filemenu = wx.Menu()    #生成一个菜单 
 
 
    qmi = wx.MenuItem(filemenu, APP_EXIT, "Quit")   #生成一个菜单项 
    qmi.SetBitmap(wx.Bitmap("2.bmp"))    #给菜单项前面加个小图标 
    filemenu.AppendItem(qmi)      #把菜单项加入到菜单中 
 
    menubar.Append(filemenu, "&File")    #把菜单加入到菜单栏中 
    self.SetMenuBar(menubar)      #把菜单栏加入到Frame框架中 
 
    self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)  #给菜单项加入事件处理 
 
    self.SetSize((300, 200))      #设置下Frame的大小,标题,和居中对齐 
    self.SetTitle("simple menu") 
    self.Centre() 
 
    self.Show(True)    #显示框架 
 
  def OnQuit(self, e):  #自定义函数 响应菜单项 
    self.Close() 
 
def main(): 
 
  ex = wx.App()      #生成一个应用程序 
  Example(None, id=-1, title="main")  #调用我们的类 
  ex.MainLoop()#消息循环 
 
if __name__ == "__main__": 
  main()

运行效果如下图所示:

python之wxPython菜单使用详解

这里再来解释下几个API,官方文档如下:

wxMenuItem* wxMenu::AppendSeparator()

Adds a separator to the end of the menu.
See also:
Append(), InsertSeparator()

wxMenuItem::wxMenuItem ( wxMenu *  parentMenu = NULL,
     int  id = wxID_SEPARATOR,
     const wxString &  text = wxEmptyString,
     const wxString &  helpString = wxEmptyString,
    wxItemKind  kind = wxITEM_NORMAL,
    wxMenu *  subMenu = NULL 
  )

Constructs a wxMenuItem object.
Menu items can be standard, or "stock menu items", or custom. For the standard menu items (such as commands to open a file, exit the program and so on, see Stock items for the full list) it is enough to specify just the stock ID and leave text and helpString empty. Some platforms (currently wxGTK only, and see the remark in SetBitmap() documentation) will also show standard bitmaps for stock menu items.
Leaving at least text empty for the stock menu items is actually strongly recommended as they will have appearance and keyboard interface (including standard accelerators) familiar to the user.
For the custom (non-stock) menu items, text must be specified and while helpString may be left empty, it's recommended to pass the item description (which is automatically shown by the library in the status bar when the menu item is selected) in this parameter.
Finally note that you can e.g. use a stock menu label without using its stock help string:
       
 // use all stock properties:
        helpMenu->Append(wxID_ABOUT);

        // use the stock label and the stock accelerator but not the stock help string:
        helpMenu->Append(wxID_ABOUT, "", "My custom help string");

        // use all stock properties except for the bitmap:
        wxMenuItem *mymenu = new wxMenuItem(helpMenu, wxID_ABOUT);
        mymenu->SetBitmap(wxArtProvider::GetBitmap(wxART_WARNING));
        helpMenu->Append(mymenu);
that is, stock properties are set independently one from the other.

Parameters:
  parentMenu  Menu that the menu item belongs to. Can be NULL if the item is going to be added to the menu later.
  id  Identifier for this menu item. May be wxID_SEPARATOR, in which case the given kind is ignored and taken to be wxITEM_SEPARATOR instead.
  text  Text for the menu item, as shown on the menu. See SetItemLabel() for more info.
  helpString  Optional help string that will be shown on the status bar.
  kind  May be wxITEM_SEPARATOR, wxITEM_NORMAL, wxITEM_CHECK or wxITEM_RADIO.
  subMenu  If non-NULL, indicates that the menu item is a submenu.

wxMenuItem* wxMenu::Append (  int  id,
     const wxString &  item = wxEmptyString,
     const wxString &  helpString = wxEmptyString,
    wxItemKind  kind = wxITEM_NORMAL 
  )     
Adds a menu item.
Parameters:
  id  The menu command identifier.
  item  The string to appear on the menu item. See wxMenuItem::SetItemLabel() for more details.
  helpString  An optional help string associated with the item. By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays this string in the status line.
  kind  May be wxITEM_SEPARATOR, wxITEM_NORMAL, wxITEM_CHECK or wxITEM_RADIO.

Example:
        m_pFileMenu->Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a new XYZ document");
or even better for stock menu items (see wxMenuItem::wxMenuItem):
        m_pFileMenu->Append(wxID_NEW, "", "Creates a new XYZ document");
Remarks:
This command can be used after the menu has been shown, as well as on initial creation of a menu or menubar.

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python中的super用法详解
May 28 Python
python迭代器与生成器详解
Mar 10 Python
Python 数据结构之堆栈实例代码
Jan 22 Python
Python解决抛小球问题 求小球下落经历的距离之和示例
Feb 01 Python
Python的numpy库中将矩阵转换为列表等函数的方法
Apr 04 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
Apr 09 Python
python导入不同目录下的自定义模块过程解析
Nov 18 Python
python global和nonlocal用法解析
Feb 03 Python
aws 通过boto3 python脚本打pach的实现方法
May 10 Python
python读写数据读写csv文件(pandas用法)
Dec 14 Python
Python NumPy灰度图像的压缩原理讲解
Aug 04 Python
关于pytest结合csv模块实现csv格式的数据驱动问题
May 30 Python
python中lambda函数 list comprehension 和 zip函数使用指南
Sep 28 #Python
python之wxPython应用实例
Sep 28 #Python
Python实现从url中提取域名的几种方法
Sep 26 #Python
Python实现的一个简单LRU cache
Sep 26 #Python
python网络编程实例简析
Sep 26 #Python
python的re模块应用实例
Sep 26 #Python
python实现自动登录人人网并访问最近来访者实例
Sep 26 #Python
You might like
PHP中$this和$that指针使用实例
2015/01/06 PHP
js中cookie的使用详细分析
2008/05/28 Javascript
js 操作符实例代码
2009/10/24 Javascript
动态创建样式表在各浏览器中的差异测试代码
2011/09/13 Javascript
JavaScript中使用document.write向页面输出内容实例
2014/10/16 Javascript
javascript HTML5文件上传FileReader API
2020/03/27 Javascript
原生js实现可爱糖果数字时间特效
2016/12/30 Javascript
JS批量替换内容中关键词为超链接
2017/02/20 Javascript
详解微信小程序Page中data数据操作和函数调用
2017/09/27 Javascript
基于zepto.js实现登录界面
2017/10/09 Javascript
解决在vue项目中,发版之后,背景图片报错,路径不对的问题
2018/03/06 Javascript
详解React+Koa实现服务端渲染(SSR)
2018/05/23 Javascript
原生JS实现$.param() 函数的方法
2018/08/10 Javascript
nodejs微信开发之授权登录+获取用户信息
2019/03/17 NodeJs
nodejs分离html文件里面的js和css的方法
2019/04/09 NodeJs
Vue 解决路由过渡动画抖动问题(实例详解)
2020/01/05 Javascript
vue.js iview打包上线后字体图标不显示解决办法
2020/01/20 Javascript
JavaScript数组排序功能简单实现
2020/05/14 Javascript
JS组件库AlloyTouch实现图片轮播过程解析
2020/05/29 Javascript
JavaScript代码简化技巧实例解析
2020/09/09 Javascript
[59:53]DOTA2-DPC中国联赛 正赛 VG vs Elephant BO3 第二场 3月6日
2021/03/11 DOTA
教你用Python脚本快速为iOS10生成图标和截屏
2016/09/22 Python
python处理按钮消息的实例详解
2017/07/11 Python
python根据list重命名文件夹里的所有文件实例
2018/10/25 Python
python Selenium实现付费音乐批量下载的实现方法
2019/01/24 Python
Series和DataFrame使用简单入门
2019/11/13 Python
HTML5新增的表单元素和属性实例解析
2014/07/07 HTML / CSS
植村秀美国官网:Shu Uemura美国
2019/03/19 全球购物
学习党课思想汇报
2013/12/29 职场文书
地球一小时倡议书
2014/04/15 职场文书
政府绩效管理实施方案
2014/05/04 职场文书
银行爱岗敬业演讲稿
2014/05/05 职场文书
网上祭先烈心得体会
2014/09/01 职场文书
2014年度工作总结报告
2014/12/15 职场文书
只用50行Python代码爬取网络美女高清图片
2021/06/02 Python
关于html选择框创建占位符的问题
2021/06/09 HTML / CSS