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中的计时器timeit的使用方法
Oct 20 Python
Python在不同目录下导入模块的实现方法
Oct 27 Python
通过Python 接口使用OpenCV的方法
Apr 02 Python
python编写暴力破解zip文档程序的实例讲解
Apr 24 Python
opencv python 2D直方图的示例代码
Jul 20 Python
Pycharm取消py脚本中SQL识别的方法
Nov 29 Python
在python中创建指定大小的多维数组方式
Nov 28 Python
python如何获取apk的packagename和activity
Jan 10 Python
关于pytorch中全连接神经网络搭建两种模式详解
Jan 14 Python
django日志默认打印request请求信息的方法示例
May 17 Python
vscode+PyQt5安装详解步骤
Aug 12 Python
python使用scapy模块实现ping扫描的过程详解
Jan 21 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 过滤英文标点符号及过滤中文标点符号代码
2014/06/12 PHP
详细解读PHP的Yii框架中登陆功能的实现
2015/08/21 PHP
phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别
2016/02/03 PHP
PHP简单获取随机数的常用方法小结
2017/06/07 PHP
Laravel 5.5官方推荐的Nginx配置学习教程
2017/10/06 PHP
jquery $.ajax入门应用一
2008/11/19 Javascript
javascript中字符串拼接需注意的问题
2010/07/13 Javascript
node.js插件nodeclipse安装图文教程
2020/10/19 Javascript
JavaScript中解决多浏览器兼容性23个问题的快速解决方法
2016/05/19 Javascript
js闭包用法实例详解
2016/12/13 Javascript
javascript中对象的定义、使用以及对象和原型链操作小结
2016/12/14 Javascript
Node.js通过身份证号验证年龄、出生日期与性别方法示例
2017/03/09 Javascript
ajax前台后台跨域请求处理方式
2018/02/08 Javascript
在vue项目中使用sass的配置方法
2018/03/20 Javascript
angular 数据绑定之[]和{{}}的区别
2018/09/25 Javascript
vue.js的双向数据绑定Object.defineProperty方法的神奇之处
2019/01/18 Javascript
解决vue组件中click事件失效的问题
2019/11/09 Javascript
手把手教你如何编译打包video.js
2020/12/09 Javascript
[57:50]DOTA2上海特级锦标赛主赛事日 - 4 胜者组决赛Secret VS Liquid第二局
2016/03/05 DOTA
Python利用多进程将大量数据放入有限内存的教程
2015/04/01 Python
Python中exit、return、sys.exit()等使用实例和区别
2015/05/28 Python
Python实现截屏的函数
2015/07/26 Python
使用Python解析JSON数据的基本方法
2015/10/15 Python
Python 查看文件的编码格式方法
2017/12/21 Python
解决pandas使用read_csv()读取文件遇到的问题
2018/06/15 Python
学习Django知识点分享
2019/09/11 Python
jupyter note 实现将数据保存为word
2020/04/14 Python
斯凯奇澳大利亚官网:SKECHERS澳大利亚
2018/03/31 全球购物
莫斯科珠宝厂官方网站:Miuz
2020/09/19 全球购物
包装类的功能、种类、常用方法
2012/01/27 面试题
经济信息管理专业大学生求职信
2013/09/27 职场文书
计算机实训报告总结
2014/11/05 职场文书
2015年爱牙日活动总结
2015/02/05 职场文书
送达通知书
2015/04/25 职场文书
鲁冰花观后感
2015/06/10 职场文书
php访问对象中的成员的实例方法
2021/11/17 PHP