2013-01-12 41 views
0

我試圖使用鄧恩的重構Menubar代碼,但總是遇到問題。下面的代碼:createMenuItem()最多需要6個參數(給定14個)

#!/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) 

我跟隨在我的腦海幾十這個時間代碼,我無法弄清楚什麼是錯的。我已經添加並刪除了menuData對象中的任何圓括號,但無濟於事。

+0

我的答案解決了您的問題嗎?如果是這樣,請接受/ upvote顯示您對我的時間的讚賞。 – bouke

回答

1

的問題是在Parenthesized forms

A parenthesized expression list yields whatever that expression list yields: if the list contains at least one comma, it yields a tuple; otherwise, it yields the single expression that makes up the expression list.

參見下面的例子:

>>> 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 

所以你的代碼需要一個可迭代的,但是由於缺少逗號,它開始遍歷字符串值"&Quit\tCtrl+Q"(14個字符)。在指定位置添加逗號可創建元組並修復代碼。

+0

非常感謝您的幫助,bouke! – pyrodney

相關問題