2017-09-16 43 views
0

我有一個從DevExpress.v17.1庫擴展XtraForm的窗體自定義構造函數的問題(用C#創建的)。它有兩個構造函數:pythonnet自定義窗體的構造函數

protected BaseForm() 
{ 
    InitializeComponent(); 
} 

protected BaseForm(IClient client) 
{ 
    InitializeComponent(); 
    ... many code 
} 

哪裏IClient是接口。 該表單具有很多依賴關係,並且它們都在庫中編譯。 當我向此表格,並試圖通過代碼來創建實例:

class TestApp(BaseForm): 

def __init__(self): 
    self.Text = "Hello World From Python" 
    self.components = System.ComponentModel.Container() 
    self.AutoScaleBaseSize = Size(5, 13) 
    self.ClientSize = Size(392, 117) 
    h = WinForms.SystemInformation.CaptionHeight 
    self.MinimumSize = Size(392, (117 + h)) 

    # Create the button 
    self.button = WinForms.Button() 
    self.button.Location = Point(160, 64) 
    self.button.Size = Size(150, 20) 
    self.button.TabIndex = 2 
    self.button.Text = "Click Me!" 

    # Register the event handler 
    self.button.Click += self.button_Click 

    # Create the text box 
    self.textbox = WinForms.TextBox() 
    self.textbox.Text = "Hello World" 
    self.textbox.TabIndex = 1 
    self.textbox.Size = Size(126, 40) 
    self.textbox.Location = Point(160, 24) 

    # Add the controls to the form 
    self.AcceptButton = self.button 
    self.Controls.Add(self.button) 
    self.Controls.Add(self.textbox) 

def button_Click(self, sender, args): 
    """Button click event handler""" 
    print ("Click") 
    WinForms.MessageBox.Show("Please do not press this button again.") 

def run(self): 
    WinForms.Application.Run(self) 

def Dispose(self): 
    self.components.Dispose() 
    WinForms.Form.Dispose(self) 

運行初始化代碼:

def main(): 
    form = TestApp() 
    form.run() 
    form.Dispose() 

if __name__ == '__main__': 
    main() 

我有一個錯誤:

Traceback (most recent call last): 
    File "C:/Users/v.khvorostianyi/PycharmProjects/CSharp/Test.py", line 141, in <module> 
    main() 
    File "C:/Users/v.khvorostianyi/PycharmProjects/CSharp/Test.py", line 85, in main 
    form = TestApp() 
TypeError: no constructor matches given arguments 

的Python 3.6.2 = ,pythonnet = 2.3.0
.NET = 4.6.1

Pro ject需要進行自動化測試,這種形式對於工作過程是必需的。 爲什麼我有這樣的錯誤?

+0

在Python類TestApp(音素表示)meen。感謝您嘗試幫助。 –

回答

1

BaseForm中的構造函數被protected訪問修飾符隱藏,只能在BaseForm及其派生類實例內部使用。因此,不能使用form = TestApp(),因爲具有空參數的構造函數是隱藏的。

至少有兩種方法來解決這個問題:

0您可以使用public訪問修飾符在BaseForm構造。

public BaseForm() 
{ 
    InitializeComponent(); 
} 

public BaseForm(IClient client) 
{ 
    InitializeComponent(); 
    //... many code 
} 

1.您可以嘗試通過在派生類中使用__new__方法重載.NET構造:即TestApp延長音素表示

def __new__(cls):   
    return BaseForm.__new__(cls)