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 回调函数和回调方法的实现分析
Mar 23 Python
Python用list或dict字段模式读取文件的方法
Jan 10 Python
在centos7中分布式部署pyspider
May 03 Python
python 请求服务器的实现代码(http请求和https请求)
May 25 Python
python根据url地址下载小文件的实例
Dec 18 Python
在Pycharm中自动添加时间日期作者等信息的方法
Jan 16 Python
matplotlib实现区域颜色填充
Mar 18 Python
基于Python执行dos命令并获取输出的结果
Dec 30 Python
python代码xml转txt实例
Mar 10 Python
Python如何操作office实现自动化及win32com.client的运用
Apr 01 Python
你应该知道的Python3.6、3.7、3.8新特性小结
May 12 Python
记一次Django响应超慢的解决过程
Sep 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缩放gif和png图透明背景变成黑色的解决方法
2014/10/14 PHP
简单解决新浪SAE无法上传文件的问题
2015/05/13 PHP
简单解决微信文章图片防盗链问题
2016/12/17 PHP
用js实现控制内容的向上向下滚动效果
2007/06/26 Javascript
js绑定事件this指向发生改变的问题解决方法
2013/04/23 Javascript
js实现简单的星级选择器提交效果适用于评论等
2013/10/18 Javascript
setInterval计时器不准的问题解决方法
2014/05/08 Javascript
js鼠标悬浮出现遮罩层的方法
2015/01/28 Javascript
利用jQuery实现WordPress中@的ID悬浮显示评论内容
2015/12/11 Javascript
基于jQuery实现仿51job城市选择功能实例代码
2016/03/02 Javascript
解决URL地址中的中文乱码问题的办法
2017/02/10 Javascript
AngularJs用户登录问题处理(交互及验证、阻止FQ处理)
2017/10/26 Javascript
JS从非数组对象转数组的方法小结
2018/03/26 Javascript
javascript实现的时间格式加8小时功能示例
2019/06/13 Javascript
vue动态加载SVG文件并修改节点数据的操作代码
2020/08/17 Javascript
详解实现vue的数据响应式原理
2021/01/20 Vue.js
Python的ORM框架SQLAlchemy入门教程
2014/04/28 Python
python中assert用法实例分析
2015/04/30 Python
Python unittest 简单实现参数化的方法
2018/11/30 Python
对python中的控制条件、循环和跳出详解
2019/06/24 Python
对python while循环和双重循环的实例详解
2019/08/23 Python
python实现画出e指数函数的图像
2019/11/21 Python
Python数据可视化:顶级绘图库plotly详解
2019/12/07 Python
python脚本后台执行方式
2019/12/21 Python
Python基于Hypothesis测试库生成测试数据
2020/04/29 Python
python爬虫如何解决图片验证码
2021/02/14 Python
CSS 3.0文字悬停跳动特效代码
2020/10/26 HTML / CSS
html5的canvas实现3d雪花飘舞效果
2013/12/27 HTML / CSS
Monki官网:斯堪的纳维亚的独立时尚品牌
2020/11/09 全球购物
销售人员自我评价怎么写
2013/09/19 职场文书
招聘与培训专员岗位职责
2014/01/30 职场文书
入党积极分子对十八届四中全会期盼的思想汇报
2014/10/17 职场文书
学生不参加考试检讨书
2015/02/19 职场文书
2016年5月份红领巾广播稿
2015/12/21 职场文书
Vue中插槽slot的使用方法与应用场景详析
2021/06/08 Vue.js
opencv用VS2013调试时用Image Watch插件查看图片
2021/07/26 Python