python之wxPython菜单使用详解
本文实例讲述了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程序设计有所帮助。
上一篇: 黑夜路人出的几道php笔试题