本文实例讲述了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 filetCTRL 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更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

Python和C 在内存管理和控制方面的差异显着。 1.Python使用自动内存管理,基于引用计数和垃圾回收,简化了程序员的工作。 2.C 则要求手动管理内存,提供更多控制权但增加了复杂性和出错风险。选择哪种语言应基于项目需求和团队技术栈。

Python在科学计算中的应用包括数据分析、机器学习、数值模拟和可视化。1.Numpy提供高效的多维数组和数学函数。2.SciPy扩展Numpy功能,提供优化和线性代数工具。3.Pandas用于数据处理和分析。4.Matplotlib用于生成各种图表和可视化结果。

选择Python还是C 取决于项目需求:1)Python适合快速开发、数据科学和脚本编写,因其简洁语法和丰富库;2)C 适用于需要高性能和底层控制的场景,如系统编程和游戏开发,因其编译型和手动内存管理。

Python在数据科学和机器学习中的应用广泛,主要依赖于其简洁性和强大的库生态系统。1)Pandas用于数据处理和分析,2)Numpy提供高效的数值计算,3)Scikit-learn用于机器学习模型构建和优化,这些库让Python成为数据科学和机器学习的理想工具。

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

Python在Web开发中的关键应用包括使用Django和Flask框架、API开发、数据分析与可视化、机器学习与AI、以及性能优化。1.Django和Flask框架:Django适合快速开发复杂应用,Flask适用于小型或高度自定义项目。2.API开发:使用Flask或DjangoRESTFramework构建RESTfulAPI。3.数据分析与可视化:利用Python处理数据并通过Web界面展示。4.机器学习与AI:Python用于构建智能Web应用。5.性能优化:通过异步编程、缓存和代码优

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6
视觉化网页开发工具