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 相关文章推荐
pycharm 使用心得(一)安装和首次使用
Jun 05 Python
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
Apr 24 Python
用Python实现命令行闹钟脚本实例
Sep 05 Python
python实现壁纸批量下载代码实例
Jan 25 Python
python实现定时提取实时日志程序
Jun 22 Python
python将字符串以utf-8格式保存在txt文件中的方法
Oct 30 Python
使用python实现简单五子棋游戏
Jun 18 Python
对Python函数设计规范详解
Jul 19 Python
一篇文章弄懂Python中的可迭代对象、迭代器和生成器
Aug 12 Python
python连接、操作mongodb数据库的方法实例详解
Sep 11 Python
python获取栅格点和面值的实现
Mar 10 Python
Python Socket TCP双端聊天功能实现过程详解
Jun 15 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后期静态绑定实例浅析
2018/12/21 PHP
PHP+Oracle本地开发环境搭建方法详解
2019/04/01 PHP
jQuery实现简单的日期输入格式化控件
2015/03/12 Javascript
JavaScript实现的伸展收缩型菜单代码
2015/10/14 Javascript
jQuery添加和删除输入文本框标签代码
2016/05/20 Javascript
AngularJS $injector 依赖注入详解
2016/09/14 Javascript
angular基于路由控制ui-router实现系统权限控制
2016/09/27 Javascript
详解vue嵌套路由-query传递参数
2017/05/23 Javascript
BootStrap selectpicker后台动态绑定数据
2017/06/01 Javascript
JavaScript的setter与getter方法
2017/11/29 Javascript
Vue项目安装插件并保存
2019/01/28 Javascript
Javascript Dom元素获取和添加详解
2019/09/24 Javascript
vue页面加载时的进度条功能(实例代码)
2020/01/13 Javascript
Node.js API详解之 net模块实例分析
2020/05/18 Javascript
微信小程序选择图片控件
2021/01/19 Javascript
python调用cmd命令行制作刷博器
2014/01/13 Python
Python中基础的socket编程实战攻略
2016/06/01 Python
python编码总结(编码类型、格式、转码)
2016/07/01 Python
连接pandas以及数组转pandas的方法
2019/06/28 Python
Python文件路径名的操作方法
2019/10/30 Python
使用python实现哈希表、字典、集合操作
2019/12/22 Python
基于Python绘制美观动态圆环图、饼图
2020/06/03 Python
使用CSS3中的calc()属性来以算式表达尺寸数值
2016/06/06 HTML / CSS
全球航班旅行搜索网站:Cheapflights
2017/05/19 全球购物
马来西亚综合购物网站:Lazada马来西亚
2018/06/05 全球购物
巴西化妆品商店:Lojas Rede
2019/07/26 全球购物
linux面试题参考答案(4)
2013/01/28 面试题
新闻专业个人自我评价
2013/09/21 职场文书
团队精神演讲稿
2013/12/31 职场文书
企业文化理念标语
2014/06/10 职场文书
篮球比赛口号
2014/06/10 职场文书
Matlab求解数组中的最大值及它所在的具体位置
2021/04/16 Python
python tkinter实现定时关机
2021/04/21 Python
Redis 异步机制
2022/05/15 Redis
Spring boot admin 服务监控利器详解
2022/08/05 Java/Android
Valheim服务器 Mod修改安装教程 【ValheimPlus】
2022/12/24 Servers