wxPython - 在菜单栏中添加子菜单
在本文中,我们将学习如何将子菜单项添加到菜单栏上的菜单项。我们可以通过 wxMenuBar 类中的相同 Append()函数来做到这一点。
Syntax: wx.MenuBar.Append(self, menu, title)
Parameters:
Parameter Input Type Description menu wx.Menu The menu to add. Do not deallocate this menu after calling Append . title string The title of the menu, must be non-empty.
Return: bool
代码示例:
Python3
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
# create MenuBar using MenuBar() function
menubar = wx.MenuBar()
# add menu to MenuBar
fileMenu = wx.Menu()
# add submenu item
fileItem = fileMenu.Append(20, 'SubMenu')
menubar.Append(fileMenu, '&Menu# 1')
self.SetMenuBar(menubar)
self.SetSize((300, 200))
self.SetTitle('Menu Bar')
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
输出 :