自动化 wxPython 主菜单设置?
Automating a wxPython main menu setup?
我正在尝试找到一种方法来压缩和自动构建主菜单(在标题栏下方,带有 file、edit, help, 等)在 wxPython.
每一个菜单项写出来都是直接的,但是我发现我重复了很多,在Appending,排序ID之间等等。后面还有其他独特的坑,比如如果我想给特定的菜单添加一个图标,或者如果我有子菜单,他们可能有子菜单,等等。如果没有一种一致的方法来逐项列出所有内容,只需将信息添加到列表或字典,或两者的组合,我的 wx.Frame object会变得很稠密。
除了 3 维数组之外,我看不到一种干净、有组织的方法。即便如此,我也不知道如何统一组织 3D 阵列,以便每个项目都准备就绪。
这是我目前的情况(请原谅任何缩进错误;它对我来说很好用):
class frameMain(wx.Frame):
"""The main application frame."""
def __init__(self,
parent=None,
id=-1,
title='TITLE',
pos=wx.DefaultPosition,
size=wx.Size(550, 400),
style=wx.DEFAULT_FRAME_STYLE):
"""Initialize the Main frame structure."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.Center()
self.CreateStatusBar()
self.buildMainMenu()
def buildMainMenu(self):
"""Creates the main menu at the top of the screen."""
MainMenu = wx.MenuBar()
# Establish menu item IDs.
menuID_File = ['exit']
menuID_Help = ['about']
menuID_ALL = [menuID_File,
menuID_Help]
# Make a dictionary of the menu item IDs.
self.menuID = {}
for eachmenu in menuID_ALL:
for eachitem in eachmenu:
self.menuID[eachitem] = wx.NewId()
# Create the menus.
MM_File = wx.Menu()
FILE = {}
MM_File.AppendSeparator()
FILE['exit'] = MM_File.Append(self.menuID['exit'],
'Exit',
'Exit application.')
self.Bind(wx.EVT_MENU, self.onExit, FILE['exit'])
MainMenu.Append(MM_File, 'File')
MM_Help = wx.Menu()
HELP = {}
MM_Help.AppendSeparator()
HELP['about'] = MM_Help.Append(self.menuID['about'],
'About',
'About the application.')
self.Bind(wx.EVT_MENU, self.onAbout, HELP['about'])
MainMenu.Append(MM_Help, 'Help')
# Install the Main Menu.
self.SetMenuBar(MainMenu)
我尝试使用 list-to-dictionary 来实现它,因此在引用 ID 时不需要特定的索引号,只需输入关键字即可获取 ID。我只写了一次,它就应用于函数的其余部分。
请注意我必须如何创建一个全新的变量并重复自身,例如 MM_File、MM_Edit、MM_Help,并且每次我都添加类似的信息以附加并绑定。请记住,有些菜单可能需要分隔符,或者菜单中有菜单,或者我可能想在这些菜单项中的任何一个旁边使用精灵,所以我想弄清楚如何组织我的数组来做到这一点.
什么是将其组织成一个简洁的系统才不会膨胀的 class?
您可以采用多种方法。如果愿意,您可以将菜单生成代码放入辅助函数中。这样的事情应该有效:
def menu_helper(self, menu, menu_id, name, help, handler, sep=True):
menu_obj = wx.Menu()
if sep:
menu_obj.AppendSeparator()
menu_item = menu_obj.Append(menu_id, name, help)
self.Bind(wx.EVT_MENU, handler, menu_item)
self.MainMenu.Append(menu_obj, menu)
这是一个完整的例子:
import wx
class frameMain(wx.Frame):
"""The main application frame."""
def __init__(self,
parent=None,
id=-1,
title='TITLE',
pos=wx.DefaultPosition,
size=wx.Size(550, 400),
style=wx.DEFAULT_FRAME_STYLE):
"""Initialize the Main frame structure."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.Center()
self.CreateStatusBar()
self.buildMainMenu()
def buildMainMenu(self):
"""Creates the main menu at the top of the screen."""
self.MainMenu = wx.MenuBar()
# Establish menu item IDs.
menuID_File = 'exit'
menuID_Help = 'about'
menuID_ALL = [menuID_File,
menuID_Help]
# Make a dictionary of the menu item IDs.
self.menuID = {item: wx.NewId() for item in menuID_ALL}
# Create the menus.
self.menu_helper('File', self.menuID['exit'], 'Exit',
'Exit application', self.onExit)
self.menu_helper('Help', self.menuID['about'], 'About',
'About the application.', self.onAbout)
# Install the Main Menu.
self.SetMenuBar(self.MainMenu)
def menu_helper(self, menu, menu_id, name, help, handler, sep=True):
"""
"""
menu_obj = wx.Menu()
if sep:
menu_obj.AppendSeparator()
menu_item = menu_obj.Append(menu_id, name, help)
self.Bind(wx.EVT_MENU, handler, menu_item)
self.MainMenu.Append(menu_obj, menu)
#----------------------------------------------------------------------
def onExit(self, event):
pass
def onAbout(self, event):
pass
if __name__ == '__main__':
app = wx.App(False)
frame = frameMain()
frame.Show()
app.MainLoop()
或者您可以创建一个 class 来处理所有菜单创建。您还可以创建一个配置文件,其中包含您阅读以创建菜单的所有这些信息。另一种选择是使用 XRC,尽管我个人认为这有点限制。
我正在尝试找到一种方法来压缩和自动构建主菜单(在标题栏下方,带有 file、edit, help, 等)在 wxPython.
每一个菜单项写出来都是直接的,但是我发现我重复了很多,在Appending,排序ID之间等等。后面还有其他独特的坑,比如如果我想给特定的菜单添加一个图标,或者如果我有子菜单,他们可能有子菜单,等等。如果没有一种一致的方法来逐项列出所有内容,只需将信息添加到列表或字典,或两者的组合,我的 wx.Frame object会变得很稠密。
除了 3 维数组之外,我看不到一种干净、有组织的方法。即便如此,我也不知道如何统一组织 3D 阵列,以便每个项目都准备就绪。
这是我目前的情况(请原谅任何缩进错误;它对我来说很好用):
class frameMain(wx.Frame):
"""The main application frame."""
def __init__(self,
parent=None,
id=-1,
title='TITLE',
pos=wx.DefaultPosition,
size=wx.Size(550, 400),
style=wx.DEFAULT_FRAME_STYLE):
"""Initialize the Main frame structure."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.Center()
self.CreateStatusBar()
self.buildMainMenu()
def buildMainMenu(self):
"""Creates the main menu at the top of the screen."""
MainMenu = wx.MenuBar()
# Establish menu item IDs.
menuID_File = ['exit']
menuID_Help = ['about']
menuID_ALL = [menuID_File,
menuID_Help]
# Make a dictionary of the menu item IDs.
self.menuID = {}
for eachmenu in menuID_ALL:
for eachitem in eachmenu:
self.menuID[eachitem] = wx.NewId()
# Create the menus.
MM_File = wx.Menu()
FILE = {}
MM_File.AppendSeparator()
FILE['exit'] = MM_File.Append(self.menuID['exit'],
'Exit',
'Exit application.')
self.Bind(wx.EVT_MENU, self.onExit, FILE['exit'])
MainMenu.Append(MM_File, 'File')
MM_Help = wx.Menu()
HELP = {}
MM_Help.AppendSeparator()
HELP['about'] = MM_Help.Append(self.menuID['about'],
'About',
'About the application.')
self.Bind(wx.EVT_MENU, self.onAbout, HELP['about'])
MainMenu.Append(MM_Help, 'Help')
# Install the Main Menu.
self.SetMenuBar(MainMenu)
我尝试使用 list-to-dictionary 来实现它,因此在引用 ID 时不需要特定的索引号,只需输入关键字即可获取 ID。我只写了一次,它就应用于函数的其余部分。
请注意我必须如何创建一个全新的变量并重复自身,例如 MM_File、MM_Edit、MM_Help,并且每次我都添加类似的信息以附加并绑定。请记住,有些菜单可能需要分隔符,或者菜单中有菜单,或者我可能想在这些菜单项中的任何一个旁边使用精灵,所以我想弄清楚如何组织我的数组来做到这一点.
什么是将其组织成一个简洁的系统才不会膨胀的 class?
您可以采用多种方法。如果愿意,您可以将菜单生成代码放入辅助函数中。这样的事情应该有效:
def menu_helper(self, menu, menu_id, name, help, handler, sep=True):
menu_obj = wx.Menu()
if sep:
menu_obj.AppendSeparator()
menu_item = menu_obj.Append(menu_id, name, help)
self.Bind(wx.EVT_MENU, handler, menu_item)
self.MainMenu.Append(menu_obj, menu)
这是一个完整的例子:
import wx
class frameMain(wx.Frame):
"""The main application frame."""
def __init__(self,
parent=None,
id=-1,
title='TITLE',
pos=wx.DefaultPosition,
size=wx.Size(550, 400),
style=wx.DEFAULT_FRAME_STYLE):
"""Initialize the Main frame structure."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.Center()
self.CreateStatusBar()
self.buildMainMenu()
def buildMainMenu(self):
"""Creates the main menu at the top of the screen."""
self.MainMenu = wx.MenuBar()
# Establish menu item IDs.
menuID_File = 'exit'
menuID_Help = 'about'
menuID_ALL = [menuID_File,
menuID_Help]
# Make a dictionary of the menu item IDs.
self.menuID = {item: wx.NewId() for item in menuID_ALL}
# Create the menus.
self.menu_helper('File', self.menuID['exit'], 'Exit',
'Exit application', self.onExit)
self.menu_helper('Help', self.menuID['about'], 'About',
'About the application.', self.onAbout)
# Install the Main Menu.
self.SetMenuBar(self.MainMenu)
def menu_helper(self, menu, menu_id, name, help, handler, sep=True):
"""
"""
menu_obj = wx.Menu()
if sep:
menu_obj.AppendSeparator()
menu_item = menu_obj.Append(menu_id, name, help)
self.Bind(wx.EVT_MENU, handler, menu_item)
self.MainMenu.Append(menu_obj, menu)
#----------------------------------------------------------------------
def onExit(self, event):
pass
def onAbout(self, event):
pass
if __name__ == '__main__':
app = wx.App(False)
frame = frameMain()
frame.Show()
app.MainLoop()
或者您可以创建一个 class 来处理所有菜单创建。您还可以创建一个配置文件,其中包含您阅读以创建菜单的所有这些信息。另一种选择是使用 XRC,尽管我个人认为这有点限制。