2011-12-05 6 views
0

wxPythonフレームを作成するWxFrameというクラスがあります。私はPyDEPPクラスのオブジェクトである自己とpydeppを受けcreateRunButtonと呼ばれる方法を、追加wx.Buttonのバインドを使用する場合のPython TypeError

import wx 

class WxFrame(wx.Frame): 
    def __init__(self, parent, title): 
     super(WxFrame, self).__init__(parent, title=title) 
     self.Maximize() 
     self.Show() 

    def createRunButton(self,pydepp): 
     #pydepp.run() 
     self.runButton = wx.Button(self, label="Run") 
     self.Bind(wx.EVT_BUTTON, pydepp.run, self.runButton 

これはPyDEPPクラスです:私はインスタンス化して、それを実行

class PyDEPP: 
    def run(self): 
     print "running" 

import wx 
from gui.gui import WxFrame 
from Depp.Depp import PyDEPP 

class PyDEPPgui(): 
    """PyDEPPgui create doc string here ....""" 
    def __init__(self,pydepp): 
     self.app = wx.App(False) 
     ##Create a wxframe and show it 
     self.frame = WxFrame(None, "Cyclic Depp Data Collector - Ver. 0.1") 
     self.frame.createRunButton(pydepp) 
     self.frame.SetStatusText('wxPython GUI successfully initialised') 

if __name__=='__main__': 
    #Launch the program by calling the PyDEPPgui __init__ constructor 
    pydepp = PyDEPP() 
    pydeppgui = PyDEPPgui(pydepp) 
    pydeppgui.app.MainLoop() 

上記のコードを実行するとエラーが発生します。 TypeError:run()は1つの引数(2が指定された)をとります

しかし、バインドをコメントアウトしてpydepp.run()行のコメントを外しても問題ありません。

答えは明らかですが、私はCompSciまたはOOコーディングを学んだことはありません。

答えて

2

イベントは、コールバック関数の引数として渡されます。これは動作するはずです:イベントは2つの引数は、コールバック関数の実行に渡されトリガされ

class PyDEPP: 
    def run(self, event): 
     print "running" 
1

():イベントをトリガしたオブジェクト、およびwxEventオブジェクト。 runはコード内で1つの引数だけを受け入れるので、インタプリタはあなたに多すぎる引数を渡したことを伝えるエラーを与えています。

run(self, event): # Expects two arguments, gets two arguments. All is well 

run(self): # Expects one argument, but is passed two. TypeError thrown 

を交換し、それが動作するはずです。

これは、エラーがコードの何が間違っているかを多く説明する1つのインスタンスです。 "run()はちょうど1つの引数(与えられた2つ)"を受け取ると、あなたは誤って余分な引数を渡したか、別の引数を期待しているはずです。

関連する問題