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实现多行注释的另类方法
Aug 22 Python
基于python 处理中文路径的终极解决方法
Apr 12 Python
python中使用psutil查看内存占用的情况
Jun 11 Python
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
Nov 10 Python
windows下安装Python虚拟环境virtualenvwrapper-win
Jun 14 Python
Python如何把多个PDF文件合并代码实例
Feb 13 Python
Python异常原理及异常捕捉实现过程解析
Mar 25 Python
Python文件时间操作步骤代码详解
Apr 13 Python
python2和python3哪个使用率高
Jun 23 Python
python中remove函数的踩坑记录
Jan 04 Python
sklearn中的交叉验证的实现(Cross-Validation)
Feb 22 Python
Python中threading库实现线程锁与释放锁
May 17 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学习笔记之php文件操作
2016/06/03 PHP
php 删除指定文件夹的实例讲解
2017/07/25 PHP
extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
2009/11/27 Javascript
几个比较经典常用的jQuery小技巧
2010/03/01 Javascript
jQuery代码优化 事件委托篇
2011/11/01 Javascript
jquery dialog open后,服务器端控件失效的快速解决方法
2013/12/19 Javascript
javascript 实现子父窗体互相传值的简单实例
2014/02/17 Javascript
js通过更改按钮的显示样式实现按钮的滑动效果
2014/04/23 Javascript
jquery用data方法获取某个元素上的事件
2014/06/23 Javascript
纯javascript响应式树形菜单效果
2015/11/10 Javascript
javascript日期验证之输入日期大于等于当前日期
2015/12/13 Javascript
微信js-sdk预览图片接口及从拍照或手机相册中选图接口用法示例
2016/10/13 Javascript
JavaScript获取tr td 的三种方式全面总结(推荐)
2017/08/15 Javascript
React styled-components设置组件属性的方法
2018/08/07 Javascript
微信小程序picker组件关于objectArray数据类型的绑定方法
2019/03/13 Javascript
详解从vue-loader源码分析CSS Scoped的实现
2019/09/23 Javascript
微信小程序iOS下拉白屏晃动问题解决方案
2019/10/12 Javascript
vue使用vue-quill-editor富文本编辑器且将图片上传到服务器的功能
2021/01/13 Vue.js
python爬虫入门教程之糗百图片爬虫代码分享
2014/09/02 Python
一个超级简单的python web程序
2014/09/11 Python
Python生成器(Generator)详解
2015/04/13 Python
python使用PIL缩放网络图片并保存的方法
2015/04/24 Python
Python加pyGame实现的简单拼图游戏实例
2015/05/15 Python
使用Python的Scrapy框架十分钟爬取美女图
2016/12/26 Python
python微信跳一跳系列之自动计算跳一跳距离
2018/02/26 Python
解决python中无法自动补全代码的问题
2018/12/04 Python
python retrying模块的使用方法详解
2019/09/25 Python
pytorch实现seq2seq时对loss进行mask的方式
2020/02/18 Python
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
2020/06/09 Python
如何解决python多种版本冲突问题
2020/10/13 Python
家乐福巴西网上超市:Carrefour巴西
2016/10/31 全球购物
副总经理工作职责
2013/11/28 职场文书
2014年护士长工作总结
2014/11/11 职场文书
医德医风个人工作总结2014
2014/11/14 职场文书
导游词之麻姑仙境
2019/11/18 职场文书
JS前端使用Canvas快速实现手势解锁特效
2022/09/23 Javascript