2012-12-27 15 views
5

我試圖使用Notebook類在wxPython中製作標籤。使用上面掛的教程中,我想出了下面的代碼:TypeError:由名稱「id」和位置(2)給出的參數 - wxPython

#!/usr/bin/env python 

import wx 

class DeployTab(wx.Panel): 
    def __init__(self, parent, *args, **kwargs): 
     super(DeployTab, self).__init__(self, *args, parent=parent, id=wx.ID_ANY, **kwargs) 

     self.sizer = wx.Panel(self) 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     deploy = wx.Button(
      self.main_panel, 
      label="test 1", 
      size=(250, 100)) 
     self.sizer.Add(deploy, flag=wx.EXPAND|wx.ALL, border=5) 

     self.SetSizer(self.sizer) 

class ConfigTab(wx.Panel): 
    # For now, copy. 
    def __init__(self, parent, *args, **kwargs): 
     super(ConfigTab, self).__init__(self, *args, parent=parent, id=wx.ID_ANY, **kwargs) 

     self.sizer = wx.Panel(self) 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     deploy = wx.Button(
      self.main_panel, 
      label="test2", 
      size=(250, 100)) 
     self.sizer.Add(deploy, flag=wx.EXPAND|wx.ALL, border=5) 

     self.SetSizer(self.sizer) 

class NotebookTabs(wx.Notebook): 
    def __init__(self, parent): 
     super(NotebookTabs, self).__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT) 

     self.deploy_tab = DeployTab(self) 
     self.deploy_tab.SetBackgroundColor("Gray") 
     self.AddPage(self.main_tab, "Go!") 

     self.options_tab = ConfigTab(self) 
     self.AddPage(self.options_tab, "Options") 


class MainWindow(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     super(MainWindow, self).__init__(*args, **kwargs) 

     self.SetSize((300, 250)) 
     self.SetTitle('Testing') 
     self.Centre() 
     self.Show(True) 

     self.setup_sizers() 

    def setup_sizers(self): 
     self.panel = wx.Panel(self) 
     self.notebook = NotebookTabs(self.panel) 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5) 
     self.panel.SetSizer(self.sizer) 
     self.Layout() 

    def on_quit(self, event): 
     self.Close() 

def main(): 
    app = wx.App() 
    MainWindow(None) 
    app.MainLoop() 

if __name__ == '__main__': 
    main() 

它提供了以下錯誤:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "test.pyw", line 72, in main 
    MainWindow(None) 
    File "test.pyw", line 55, in __init__ 
    self.setup_sizers() 
    File "test.pyw", line 61, in setup_sizers 
    self.notebook = NotebookTabs(self.panel) 
    File "test.pyw", line 36, in __init__ 
    super(NotebookTabs, self).__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT) 
    File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 3147, in __init__ 
    _controls_.Notebook_swiginit(self,_controls_.new_Notebook(*args, **kwargs)) 
TypeError: Argument given by name ('id') and position (2) 

我再變NotebookTabs以下(簡化了超類初始化):

class NotebookTabs(wx.Notebook): 
    def __init__(self, parent): 
     super(NotebookTabs, self).__init__(self, parent) 

     self.deploy_tab = DeployTab(self) 
     self.deploy_tab.SetBackgroundColor("Gray") 
     self.AddPage(self.main_tab, "Go!") 

     self.options_tab = ConfigTab(self) 
     self.AddPage(self.options_tab, "Options") 

...並得到了以下錯誤消息:

File "<stdin>", line 1, in <module> 
    File "test.pyw", line 72, in main 
    MainWindow(None) 
    File "test.pyw", line 55, in __init__ 
    self.setup_sizers() 
    File "test.pyw", line 61, in setup_sizers 
    self.notebook = NotebookTabs(self.panel) 
    File "test.pyw", line 36, in __init__ 
    super(NotebookTabs, self).__init__(self, parent) 
    File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 3147, in __init__ 
    _controls_.Notebook_swiginit(self,_controls_.new_Notebook(*args, **kwargs)) 
TypeError: in method 'new_Notebook', expected argument 1 of type 'wxWindows *' 

我覺得我錯過了一些明顯的東西,但我似乎無法確定哪裏出了問題。有人能幫我找出問題嗎?

回答

4

你不應該傳遞到selfsuper(NotebookTabs, self).__init__super需要照顧的是:

super(NotebookTabs, self).__init__(parent) 

super(DeployTab, self).__init__(*args, parent=parent, id=wx.ID_ANY, **kwargs) 

super(ConfigTab, self).__init__(*args, parent=parent, id=wx.ID_ANY, **kwargs) 
2

當你通過在這兩個位置和關鍵字參數相同的參數,就會出現此問題:

def foo(x=10, y=20): 
    ... 

args = [5] 
kwargs = { "x":15 } 
foo(*args, **kwargs) # x is passed twice 

我不熟悉的wxPython,所以我不能確定究竟在何處發生這種情況,但我d建議檢查你的代碼在哪裏使用*args和/或**kwargs,因爲問題肯定存在。

更新:作爲@Pavel Anossov指出的那樣,問題是重複使用的self

super(NotebookTabs, self).__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT) 

調用這種方式,self將是第一個參數(parent)和parent將是第二(id)。而且你也通過id作爲關鍵字參數,因此錯誤。

相關問題