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 dict remove数组删除(del,pop)
Mar 24 Python
Python的Bottle框架中获取制定cookie的教程
Apr 24 Python
Python实现PS滤镜的万花筒效果示例
Jan 23 Python
深入浅析python with语句简介
Apr 11 Python
Python音频操作工具PyAudio上手教程详解
Jun 26 Python
Pycharm简单使用教程(入门小结)
Jul 04 Python
pycharm运行scrapy过程图解
Nov 22 Python
Python安装依赖(包)模块方法详解
Feb 14 Python
python 瀑布线指标编写实例
Jun 03 Python
Python TestSuite生成测试报告过程解析
Jul 23 Python
PyQt5的QWebEngineView使用示例
Oct 20 Python
Python实现简单猜数字游戏
Feb 03 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实现异步调用方法研究与分享
2011/10/27 PHP
mongo Table类文件 获取MongoCursor(游标)的实现方法分析
2013/07/01 PHP
新浪SAE云平台下使用codeigniter的数据库配置
2014/06/12 PHP
ThinkPHP3.1新特性之查询条件预处理简介
2014/06/19 PHP
Destoon模板制作简明教程
2014/06/20 PHP
php-fpm添加service服务的例子
2018/04/27 PHP
PHP常用日期加减计算方法实例小结
2018/07/31 PHP
基于jquery实现后台左侧菜单点击上下滑动显示
2013/04/11 Javascript
js 实现菜单左右滚动显示示例介绍
2013/11/21 Javascript
js 阻止子元素响应父元素的onmouseout事件具体实现
2013/12/23 Javascript
javascript三元运算符用法实例
2015/04/16 Javascript
jQuery实现的向下图文信息滚动效果
2015/05/03 Javascript
Javascript中的作用域和上下文深入理解
2015/07/03 Javascript
详解javascript事件冒泡
2016/01/09 Javascript
Vue自定义指令详解
2017/07/28 Javascript
node.js到底要不要加分号浅析
2018/07/11 Javascript
vue里input根据value改变背景色的实例
2018/09/29 Javascript
javascript导出csv文件(excel)的方法示例
2019/08/25 Javascript
vue-router 2.0 跳转之router.push()用法说明
2020/08/12 Javascript
python获取当前时间对应unix时间戳的方法
2015/05/15 Python
Python实现树的先序、中序、后序排序算法示例
2017/06/23 Python
对python中字典keys,values,items的使用详解
2019/02/03 Python
PyQt5响应回车事件的方法
2019/06/25 Python
Python GUI编程学习笔记之tkinter事件绑定操作详解
2020/03/30 Python
Django框架获取form表单数据方式总结
2020/04/22 Python
Python设计密码强度校验程序
2020/07/30 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
2020/09/01 Python
CSS3实现的文本3D效果附图
2014/09/03 HTML / CSS
仿CSDN Blog返回页面顶部功能实现原理及代码
2013/06/30 HTML / CSS
用HTML5 实现橡皮擦的涂抹效果的教程
2015/05/11 HTML / CSS
KOHLER科勒美国官网:国际著名卫浴橱柜领先品牌
2020/06/27 全球购物
幼儿园教师培训制度
2014/01/16 职场文书
力学专业求职信
2014/07/23 职场文书
2014办公室副主任四风对照检查材料思想汇报
2014/09/20 职场文书
会计稽核岗位职责
2015/04/13 职场文书
使用Vue3+Vant组件实现App搜索历史记录功能(示例代码)
2021/06/09 Vue.js