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利用elaphe制作二维条形码实现代码
May 25 Python
Python学习笔记_数据排序方法
May 22 Python
python有证书的加密解密实现方法
Nov 19 Python
Python使用正则表达式获取网页中所需要的信息
Jan 29 Python
python实现关键词提取的示例讲解
Apr 28 Python
Python 抓取微信公众号账号信息的方法
Jun 14 Python
Python使用字典实现的简单记事本功能示例
Aug 15 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 Python
如何将PySpark导入Python的放实现(2种)
Apr 26 Python
matplotlib subplot绘制多个子图的方法示例
Jul 28 Python
Python3使用Selenium获取session和token方法详解
Feb 16 Python
python3 删除所有自定义变量的操作
Apr 08 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-5.2 的 php.ini 中文版[金步国翻译]
2011/04/17 PHP
PHP大批量数据操作时临时调整内存与执行时间的方法
2011/04/20 PHP
PHP防盗链的基本思想 防盗链的设置方法
2015/09/25 PHP
PHP链表操作简单示例
2016/10/15 PHP
php 截取GBK文档某个位置开始的n个字符方法
2017/03/08 PHP
PHP PDOStatement::fetchColumn讲解
2019/01/31 PHP
[原创]后缀就扩展名为js的文件是什么文件
2007/12/06 Javascript
JavaScript 开发规范要求(图文并茂)
2010/06/11 Javascript
javascript中[]和{}对象使用介绍
2013/03/20 Javascript
A标签中通过href和onclick传递的this对象实现思路
2013/04/19 Javascript
Firefox中使用outerHTML的2种解决方法
2014/06/07 Javascript
Dojo Javascript 编程规范 规范自己的JavaScript书写
2014/10/26 Javascript
JavaScript类型检测之typeof 和 instanceof 的缺陷与优化
2016/01/13 Javascript
去除字符串左右两边的空格(实现代码)
2016/05/12 Javascript
nodejs multer实现文件上传与下载
2017/05/10 NodeJs
animate.css在vue项目中的使用教程
2018/08/05 Javascript
实例详解vue.js浅度监听和深度监听及watch用法
2018/08/16 Javascript
Vue插值、表达式、分隔符、指令知识小结
2018/10/12 Javascript
微信小程序自定义键盘 内部虚拟支付
2018/12/20 Javascript
LayUI数据接口返回实体封装的例子
2019/09/12 Javascript
Vue实现base64编码图片间的切换功能
2019/12/04 Javascript
浅析vue cli3 封装Svgicon组件正确姿势(推荐)
2020/04/27 Javascript
Python判断以什么结尾以什么开头的实例
2018/10/27 Python
PyTorch如何搭建一个简单的网络
2020/08/24 Python
MVMT手表官方网站:时尚又实惠的高品质手表
2016/12/04 全球购物
软件设计的目标是什么
2016/12/04 面试题
信息工程学院毕业生推荐信
2013/11/05 职场文书
《晏子使楚》教学反思
2014/02/08 职场文书
汽车维修求职信
2014/06/15 职场文书
2014年“世界无车日”活动方案
2014/09/21 职场文书
高中学生自我评价范文
2014/09/23 职场文书
2015年大学生工作总结
2015/04/21 职场文书
大学生实习证明
2015/06/16 职场文书
学校团代会开幕词
2016/03/04 职场文书
Nginx实现高可用集群构建(Keepalived+Haproxy+Nginx)
2021/05/27 Servers
OpenCV项目实践之停车场车位实时检测
2022/04/11 Python