python之wxPython菜单使用详解

 更新时间:2014年09月28日 09:41:41   投稿:shichen2014  
这篇文章主要介绍了python中wxPython菜单使用方法,可实现给弹出菜单项添加图标的功能,在Python程序设计中非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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() 

运行效果如下图所示:

这里再来解释下几个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程序设计有所帮助。

相关文章

  • PyCharm 2021.2 (Professional)调试远程服务器程序的操作技巧

    PyCharm 2021.2 (Professional)调试远程服务器程序的操作技巧

    本文给大家分享用 PyCharm 2021 调试远程服务器程序的过程,通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-08-08
  • Python面向对象编程(一)

    Python面向对象编程(一)

    本文详细讲解了Python的面向对象编程,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • python随机生成指定长度密码的方法

    python随机生成指定长度密码的方法

    这篇文章主要介绍了python随机生成指定长度密码的方法,涉及Python操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • 支持python的分布式计算框架Ray详解

    支持python的分布式计算框架Ray详解

    Ray是一种分布式执行框架,便于大规模应用程序和利用先进的机器学习库,今天给大家分享支持python的分布式计算框架Ray详解,感兴趣的朋友一起看看吧
    2021-07-07
  • Python下划线5种含义代码实例解析

    Python下划线5种含义代码实例解析

    这篇文章主要介绍了Python下划线5种含义实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Python中pywifi模块的基本用法讲解

    Python中pywifi模块的基本用法讲解

    跨平台的pywifi模块支持操作无线网卡,该模块易于使用,同时支持Windows、Linux等多个系统,这篇文章主要介绍了Python中pywifi模块的基本用法,需要的朋友可以参考下
    2022-11-11
  • django在开发中取消外键约束的实现

    django在开发中取消外键约束的实现

    这篇文章主要介绍了django在开发中取消外键约束的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Python优先队列实现方法示例

    Python优先队列实现方法示例

    这篇文章主要介绍了Python优先队列实现方法,结合实例形式分析了Python优先队列的具体定义与使用方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2017-09-09
  • Python编程实现粒子群算法(PSO)详解

    Python编程实现粒子群算法(PSO)详解

    这篇文章主要介绍了Python编程实现粒子群算法(PSO)详解,涉及粒子群算法的原理,过程,以及实现代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Python3正则匹配re.split,re.finditer及re.findall函数用法详解

    Python3正则匹配re.split,re.finditer及re.findall函数用法详解

    这篇文章主要介绍了Python3正则匹配re.split,re.finditer及re.findall函数用法,结合实例形式详细分析了正则匹配re.split,re.finditer及re.findall函数的概念、参数、用法及操作注意事项,需要的朋友可以参考下
    2018-06-06

最新评论