Python createMenuItem()最多接受6个参数(给定14个)
我试图使用Dunn的重构菜单栏代码,但总是遇到问题。代码如下:Python createMenuItem()最多接受6个参数(给定14个),python,wxpython,typeerror,menubar,Python,Wxpython,Typeerror,Menubar,我试图使用Dunn的重构菜单栏代码,但总是遇到问题。代码如下: #!/usr/bin/env python import wx class Darkscreen(wx.Frame): def __init__(self, parent, id): style = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, parent, id, 'Darkscreen10b1', size=(340, 200), s
#!/usr/bin/env python
import wx
class Darkscreen(wx.Frame):
def __init__(self, parent, id):
style = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, parent, id, 'Darkscreen10b1', size=(340, 200), style=style)
panel = wx.Panel(self, -1)
panel.SetBackgroundColour("Black")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.createMenuBar()
self.Centre()
def menuData(self):
return [("&File", (
("&Quit\tCtrl+Q", "Quit", self.OnCloseWindow))),
("&Color", (
("&Black", "", self.OnBlack, wx.ITEM_RADIO),
("&White", "", self.OnWhite, wx.ITEM_RADIO),
("&Red", "", self.OnRed, wx.ITEM_RADIO),
("&Green", "", self.OnGreen, wx.ITEM_RADIO),
("&Blue", "", self.OnBlue, wx.ITEM_RADIO)))]
def createMenuBar(self):
menuBar = wx.MenuBar()
for eachMenuData in self.menuData():
menuLabel = eachMenuData[0]
menuItems = eachMenuData[1]
menuBar.Append(self.createMenu(menuItems), menuLabel)
self.SetMenuBar(menuBar)
def createMenu(self, menuData):
menu = wx.Menu()
for eachItem in menuData:
if len(eachItem) == 2:
label = eachItem[0]
subMenu = self.createMenu(eachItem[1])
menu.AppendMenu(wx.NewId(), label, subMenu)
else:
self.createMenuItem(menu, *eachItem)
return menu
def createMenuItem(self, menu, label, status, handler, kind=wx.ITEM_NORMAL):
if not label:
menu.AppendSeparator()
return
menuItem = menu.Append(-1, label, status, kind)
self.Bind(wx.EVT_MENU, handler, menuItem)
def OnBlack(self, event):
panel.SetBackgroundColour("Black")
def OnWhite(self, event):
panel.SetBackgroundColour("White")
def OnRed(self, event):
panel.SetBackgroundColour("Red")
def OnGreen(self, event):
panel.SetBackgroundColour("Green")
def OnBlue(self, event):
panel.SetBackgroundColour("Blue")
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.App(redirect=True, filename="dserr.txt")
frame = Darkscreen(parent=None, id=-1)
frame.Show()
app.MainLoop()
以下是错误:
Traceback (most recent call last):
File "L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py", line 67, in <module>
frame = Darkscreen(parent=None, id=-1)
File "L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py", line 13, in __init__
self.createMenuBar()
File "L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py", line 31, in createMenuBar
menuBar.Append(self.createMenu(menuItems), menuLabel)
File "L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py", line 42, in createMenu
self.createMenuItem(menu, *eachItem)
TypeError: createMenuItem() takes at most 6 arguments (14 given)
回溯(最近一次呼叫最后一次):
文件“L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py”,第67行,在
帧=黑色屏幕(父项=无,id=-1)
文件“L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py”,第13行,在\uuu init中__
self.createMenuBar()
文件“L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py”,第31行,在createMenuBar中
menuBar.Append(self.createMenu(menuItems)、menuLabel)
文件“L:\Rodney's\Development\Python\Working\darkscreen\darkscreen.py”,第42行,在createMenu中
self.createMenuItem(菜单,*eachItem)
TypeError:createMenuItem()最多接受6个参数(给定14个)
我已经在头脑中遵循这个代码几十次了,我无法找出哪里出了问题。我在menuData对象中的每个位置都添加和删除了括号,但没有任何效果。问题在于:
带括号的表达式列表产生该表达式列表产生的结果:如果列表至少包含一个逗号,则产生一个元组;否则,它将生成组成表达式列表的单个表达式
另请参见此示例:
>>> for x in ('hello'):
... print x
...
h
e
l
l
o
添加逗号将创建元组:
>>> for x in ('hello',):
... print x
...
hello
您的第一个列表项如下:
("&File", (("&Quit\tCtrl+Q", "Quit", self.OnCloseWindow) )
# ^-- no comma here
因此,您的代码需要一个iterable,但由于缺少逗号,它开始在字符串值
“&Quit\tCtrl+Q”
(14个字符)上迭代。在指示的位置添加逗号将创建元组并修复代码。我的回答解决了您的问题吗?如果是的话,请接受/投票以表示您对我的时间的感谢。非常感谢您的帮助,bouke!