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格式化压缩后的JS文件的方法
Mar 05 Python
通过代码实例展示Python中列表生成式的用法
Mar 31 Python
Python中title()方法的使用简介
May 20 Python
Python 内置函数complex详解
Oct 23 Python
Python爬虫获取图片并下载保存至本地的实例
Jun 01 Python
python一键去抖音视频水印工具
Sep 14 Python
使用python PIL库实现简单验证码的去噪方法步骤
May 10 Python
8段用于数据清洗Python代码(小结)
Oct 31 Python
flask框架自定义过滤器示例【markdown文件读取和展示功能】
Nov 08 Python
python实现mask矩阵示例(根据列表所给元素)
Jul 30 Python
Python实现区域填充的示例代码
Feb 03 Python
Python中快速掌握Data Frame的常用操作
Mar 31 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面向对象的使用教程 简单数据库连接
2006/11/25 PHP
php中global和$GLOBALS[]的分析之一
2012/02/02 PHP
PHP判断手机是IOS还是Android
2015/12/09 PHP
微信JSSDK分享功能图文实例详解
2019/04/08 PHP
php框架知识点的整理和补充
2021/03/01 PHP
javascript 网页跳转的方法
2008/12/24 Javascript
学习ExtJS(一) 之基础前提
2009/10/07 Javascript
10款新鲜出炉的 jQuery 插件(Ajax 插件,有幻灯片、图片画廊、菜单等)
2011/06/08 Javascript
js加强的经典分页实例
2013/03/15 Javascript
JavaScript语言核心数据类型和变量使用介绍
2013/08/23 Javascript
javascript实现颜色渐变的方法
2013/10/30 Javascript
JS实现重新加载当前页面
2016/11/29 Javascript
微信小程序开发之Tabbar实例详解
2017/01/09 Javascript
Vue中组件之间数据的传递的示例代码
2017/09/08 Javascript
vue-cli之router基本使用方法详解
2017/10/17 Javascript
vue中eventbus被多次触发以及踩过的坑
2017/12/02 Javascript
webpack配置打包后图片路径出错的解决
2018/04/26 Javascript
Node.js事件的正确使用方法
2019/04/05 Javascript
JS根据json数组多个字段排序及json数组常用操作
2019/06/06 Javascript
JS async 函数的含义和用法实例总结
2020/04/08 Javascript
利用Python中的mock库对Python代码进行模拟测试
2015/04/16 Python
Python3.x版本中新的字符串格式化方法
2015/04/24 Python
使用python实现rsa算法代码
2016/02/17 Python
Python实现图片转字符画的示例
2017/08/22 Python
Django密码系统实现过程详解
2019/07/19 Python
Python面向对象之Web静态服务器
2019/09/03 Python
python实现网站微信登录的示例代码
2019/09/18 Python
Michael Kors澳大利亚官网:世界知名的奢侈饰品和成衣设计师
2020/02/13 全球购物
医药销售求职信范文
2014/02/01 职场文书
节水倡议书范文
2014/04/15 职场文书
2014年文明创建工作总结
2014/11/25 职场文书
2015年领导干部廉洁自律工作总结
2015/05/26 职场文书
遇事可以测出您的见识与格局
2019/09/16 职场文书
Nginx反爬虫策略,防止UA抓取网站
2021/03/31 Servers
django注册用邮箱发送验证码的实现
2021/04/18 Python
了解Kubernetes中的Service和Endpoint
2022/04/01 Servers