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_SEPARATORwxITEM_NORMALwxITEM_CHECK or wxITEM_RADIO.
  subMenu If non-NULL, indicates that the menu item is a submenu.



1. 

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_SEPARATORwxITEM_NORMALwxITEM_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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python3和wxPython是一对非常强大、灵活和易用的技术组合。Python3是一种高级编程语言,有许多优秀的特性,包括代码简洁、易读、易维护、强类型支持、面向对象编程等,被广泛用于Web应用程序、人工智能、科学计算等领域。wxPython是一个开源的Python界面工具包,它允许开发者使用Python编写桌面应用程序,并提供了丰富的构建工具、图像控件、数据输入输出等。 Python3与wxPython结合使用的主要好处是开发速度非常快、效率非常高。开发者可以使用Python编写桌面应用程序,然后使用wxPython提供的丰富的构建工具,轻松地在应用程序中增加各种控件,例如文本框、按钮、复选框、单选框、列表框等。此外,wxPython还可以实现许多高级功能,例如进度条、对话框、菜单、工具栏等。 此外,使用Python3和wxPython还有其他一些好处。它们不需要太多的系统资源,通常可以在较旧的计算机上运行。它们支持跨平台,可以在Windows、Mac和Linux等不同的操作系统上运行。这些特性使Python3和wxPython成为构建桌面应用程序的首选工具组合。 最后,Python3和wxPython还存在一些缺点。例如,使用Python编写的应用程序通常比编写C ++或Java等更慢。此外,Python3和wxPython有着复杂的语法和调试等困难。支持库是稀缺的,所以开发者可能需要在自己的项目中编写大量代码。 总之,Python3和wxPython是一对非常强大、高效、易用的工具组合,被广泛使用于桌面应用程序开发、Web开发、人工智能、科学计算等领域。虽然存在一些缺点,但是它们的优点远大于缺点,因此它们非常适合初学者和专业开发者使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值