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解释器理解Python中的字节码
Apr 01 Python
实例Python处理XML文件的方法
Aug 31 Python
一波神奇的Python语句、函数与方法的使用技巧总结
Dec 08 Python
Python安装图文教程 Pycharm安装教程
Mar 27 Python
python获取文件路径、文件名、后缀名的实例
Apr 23 Python
python修改字典键(key)的方法
Aug 05 Python
初次部署django+gunicorn+nginx的方法步骤
Sep 11 Python
PyCharm下载和安装详细步骤
Dec 17 Python
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
Feb 27 Python
python 穷举指定长度的密码例子
Apr 02 Python
升级keras解决load_weights()中的未定义skip_mismatch关键字问题
Jun 12 Python
matplotlib 画双轴子图无法显示x轴的解决方法
Jul 27 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去掉从word直接粘贴过来的没有用格式的函数
2012/10/29 PHP
ThinkPHP5.0 图片上传生成缩略图实例代码说明
2018/06/20 PHP
JavaScript的类型转换(字符转数字 数字转字符)
2010/08/30 Javascript
javascript实现的使用方向键控制光标在table单元格中切换
2010/11/17 Javascript
ajax更新数据后,jquery、jq失效问题
2011/03/16 Javascript
jquery text()方法取标签中的文本
2014/07/25 Javascript
JavaScript显示表单内元素数量的方法
2015/04/02 Javascript
AngularJS基础教程之简单介绍
2015/09/27 Javascript
jQuery实现仿微软首页感应鼠标变化滑动窗口效果
2015/10/08 Javascript
jQuery插件之Tocify动态节点目录菜单生成器附源码下载
2016/01/08 Javascript
JavaScript的React框架中的JSX语法学习入门教程
2016/03/05 Javascript
JS使用Date对象实时显示当前系统时间简单示例
2018/08/23 Javascript
使用 node.js 模仿 Apache 小部分功能
2019/07/07 Javascript
微信小程序实现比较功能的方法汇总(五种方法)
2020/03/07 Javascript
jQuery实现点击滚动到指定元素上的方法分析
2020/03/19 jQuery
微信小程序学习总结(四)事件与冒泡实例分析
2020/06/04 Javascript
Nodejs环境实现socket通信过程解析
2020/07/03 NodeJs
如何运行Python程序的方法
2013/04/21 Python
python避免死锁方法实例分析
2015/06/04 Python
简单讲解Python中的闭包
2015/08/11 Python
Python  pip安装lxml出错的问题解决办法
2017/02/10 Python
对pandas的dataframe绘图并保存的实现方法
2017/08/05 Python
PyQt5每天必学之QSplitter实现窗口分隔
2018/04/19 Python
Flask web开发处理POST请求实现(登录案例)
2018/07/26 Python
Python 将 QQ 好友头像生成祝福语的实现代码
2020/05/03 Python
详解python3类型注释annotations实用案例
2021/01/20 Python
欧洲最大的拼图游戏商店:JigsawPuzzle.co.uk
2018/07/04 全球购物
巴西儿童时尚购物网站:Dinda
2019/08/14 全球购物
涉密人员保密承诺书
2014/05/28 职场文书
银行纠风工作实施方案
2014/06/08 职场文书
2014党员批评和自我批评思想汇报
2014/09/21 职场文书
2014年底个人工作总结
2015/03/10 职场文书
会计试用期自我评价
2015/03/10 职场文书
推荐信范文大全
2015/03/27 职场文书
SQL Server中的逻辑函数介绍
2022/05/25 SQL Server
Win10玩csgo闪退如何解决?Win10玩csgo闪退的解决方法
2022/07/23 数码科技