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 25 Python
python里大整数相乘相关技巧指南
Sep 12 Python
python类继承与子类实例初始化用法分析
Apr 17 Python
Python 正则表达式入门(初级篇)
Dec 07 Python
python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法
May 24 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
May 17 Python
python微元法计算函数曲线长度的方法
Nov 08 Python
Python 类属性与实例属性,类对象与实例对象用法分析
Sep 20 Python
pygame实现五子棋游戏
Oct 29 Python
Pytorch中Tensor与各种图像格式的相互转化详解
Dec 26 Python
Django实现内容缓存实例方法
Jun 30 Python
QT5 Designer 打不开的问题及解决方法
Aug 20 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
东芝TOSHIBA RP-F11电路分析
2021/03/02 无线电
php调用C代码的实现方法
2014/03/11 PHP
php多任务程序实例解析
2014/07/19 PHP
thinkphp学习笔记之多表查询
2014/07/28 PHP
PHP移动文件指针ftell()、fseek()、rewind()函数总结
2014/11/18 PHP
php获取QQ头像并显示的方法
2014/12/23 PHP
Laravel中使用阿里云OSS Composer包分享
2015/02/10 PHP
ThinkPHP模型详解
2015/07/27 PHP
Zend Framework教程之配置文件application.ini解析
2016/03/10 PHP
Symfony2框架创建项目与模板设置实例详解
2016/03/17 PHP
PHP控制反转(IOC)和依赖注入(DI)
2017/03/13 PHP
php中时间函数date及常用的时间计算
2017/05/12 PHP
PHP实现的简单组词算法示例
2018/04/10 PHP
HTML TO JavaScript 转换
2006/06/26 Javascript
javascript初学者常用技巧
2014/09/02 Javascript
javascript实现切换td中的值
2014/12/05 Javascript
jquery分析文本里url或邮件地址为真实链接的方法
2015/06/20 Javascript
jQuery动态生成Bootstrap表格
2016/11/01 Javascript
jQuery实现表单动态加减、ajax表单提交功能
2018/06/08 jQuery
在Vue组件中获取全局的点击事件方法
2018/09/06 Javascript
ES6函数和数组用法实例分析
2020/05/23 Javascript
基于Vue.js+Nuxt开发自定义弹出层组件
2020/10/09 Javascript
python开启debug模式的方法
2019/06/27 Python
详解Python中的正斜杠与反斜杠
2019/08/09 Python
在django中自定义字段Field详解
2019/12/03 Python
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
2020/02/27 Python
Django调用支付宝接口代码实例详解
2020/04/04 Python
法国体育用品商店:GO Sport
2019/10/23 全球购物
酒店管理专业毕业生推荐信
2013/11/10 职场文书
水利局群众路线专题民主生活会发言材料
2014/09/21 职场文书
教师年度个人总结
2015/02/11 职场文书
校园音乐节目广播稿
2015/08/19 职场文书
企业文化学习心得体会
2016/01/21 职场文书
2019同学聚会主持词
2019/05/06 职场文书
详解Apache SkyWalking 告警配置指南
2021/04/22 Servers
Python基础之操作MySQL数据库
2021/05/06 Python