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通过shutil实现快速文件复制的方法
Mar 14 Python
Python中统计函数运行耗时的方法
May 05 Python
Python中使用插入排序算法的简单分析与代码示例
May 04 Python
Python黑魔法@property装饰器的使用技巧解析
Jun 16 Python
python 性能提升的几种方法
Jul 15 Python
Python3.x爬虫下载网页图片的实例讲解
May 22 Python
使用pyinstaller打包PyQt4程序遇到的问题及解决方法
Jun 24 Python
Python 获取ftp服务器文件时间的方法
Jul 02 Python
选择Python写网络爬虫的优势和理由
Jul 07 Python
在macOS上搭建python环境的实现方法
Aug 13 Python
tensorflow dataset.shuffle、dataset.batch、dataset.repeat顺序区别详解
Jun 03 Python
pandas时间序列之pd.to_datetime()的实现
Jun 16 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
apache+mysql+php+ssl服务器之完全安装攻略
2006/09/05 PHP
初学PHP的朋友 经常问的一些问题。不断更新
2011/08/11 PHP
php-cli简介(不会Shell语言一样用Shell)
2013/06/03 PHP
保存到桌面、设为桌面且带图标的PHP代码
2013/11/19 PHP
ThinkPHP实现简单登陆功能
2017/04/28 PHP
对laravel的csrf 防御机制详解,及form中csrf_token()的存在介绍
2019/10/24 PHP
javascript 页面划词搜索JS
2009/09/28 Javascript
js替代copy(示例代码)
2013/11/27 Javascript
JavaScript中一个奇葩的IE浏览器判断方法
2014/04/16 Javascript
jQuery实现列表自动滚动循环滚动展示新闻
2014/08/22 Javascript
一个获取第n个元素节点的js函数
2014/09/02 Javascript
javascript动态控制服务器控件实例
2014/09/05 Javascript
基于jquery css3实现点击动画弹出表单源码特效
2015/08/31 Javascript
jstl中判断list中是否包含某个值的简单方法
2016/10/14 Javascript
require、backbone等重构手机图片查看器
2016/11/17 Javascript
JS产生随机数的用法小结
2016/12/10 Javascript
深入学习jQuery中的data()
2016/12/22 Javascript
RequireJS 依赖关系的实例(推荐)
2017/01/21 Javascript
Angular2 路由问题修复详解
2017/03/01 Javascript
浅谈javascript的url参数parse和build函数
2017/03/04 Javascript
Vue-cli proxyTable 解决开发环境的跨域问题详解
2017/05/18 Javascript
tensorflow 1.0用CNN进行图像分类
2018/04/15 Python
Python中偏函数用法示例
2018/06/07 Python
Python3非对称加密算法RSA实例详解
2018/12/06 Python
树莓派+摄像头实现对移动物体的检测
2019/06/22 Python
如何利用python 读取配置文件
2021/01/06 Python
英国最受欢迎的价格比较网站之一:MoneySuperMarket
2018/12/19 全球购物
求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")
2015/09/11 面试题
教师个人鉴定材料
2014/02/08 职场文书
求职自我推荐信
2015/03/24 职场文书
实施意见格式范本
2015/06/05 职场文书
会计专业自荐信范文
2019/05/22 职场文书
2019中小学生安全过暑期倡议书
2019/06/24 职场文书
劳务派遣管理制度(样本)
2019/08/23 职场文书
Python Django 后台管理之后台模型属性详解
2021/04/25 Python
Docker下安装Oracle19c
2022/04/13 Servers