Eto.Forms class inheritance and initialization argument issue

Hello :wave: I’m trying to create a simple dialog extending the Eto.Forms.Form as a base class and adding a couple arguments in the constructor for width and height maybe i need to learn more about inheritance but my basic examples seems like they should work :man_shrugging:

I haven’t gone through the entire Inheritance Hierarchy yet to check for issues…

System.Object
Eto.Widget
Eto.Forms.BindableWidget
Eto.Forms.Control
Eto.Forms.Container
Eto.Forms.Panel
Eto.Forms.Window
Eto.Forms.Form

but… I’ve tried several variations of class initialization with no luck always getting the same error:

Message: WebDialog() takes at most 2 arguments (3 given)

Test 1 (Failed)

This was the most obvious and basic…:sob:

import Eto.Forms as forms
import Eto.Drawing as drawing

class WebDialog(forms.Form):
    def __init__(self, width=400, height=200):
        self.Size = drawing.Size(width, height)
        self.Title = "Test WebDialog"

if __name__ == "__main__":
    dialog = WebDialog(500, 500)
    dialog.Show()

Test 2 (Failed)

Trying *args and **kwargs:confounded:

import Eto.Forms as forms
import Eto.Drawing as drawing

class WebDialog(forms.Form):
    def __init__(self, *args, **kwargs):
        width = kwargs.get('width', 400)
        height = kwargs.get('height', 200)
        self.Size = drawing.Size(width, height)
        self.Title = "Test WebDialog"

if __name__ == "__main__":
    dialog = WebDialog(width=500, height=500)
    dialog.Show()

Test 3 (Failed)

I tried explicitly initializing the base class at the beginning of the constructor…:unamused:

import Eto.Forms as forms
import Eto.Drawing as drawing

class WebDialog(forms.Form):
    def __init__(self, width=400, height=200):
        forms.Form.__init__(self)
        self.Size = drawing.Size(width, height)
        self.Title = "Test WebDialog"

if __name__ == "__main__":
    dialog = WebDialog(500, 500)
    dialog.Show()

Test 4 (Failed)

I thought ok let me just use super() to call the initializer of the base class forms.Form and override the __init__ to ensure that the base class is properly initialized…:weary:

import Eto.Forms as forms
import Eto.Drawing as drawing

class WebDialog(forms.Form):
    def __init__(self, width=400, height=200):
        super(WebDialog, self).__init__()
        self.Size = drawing.Size(width, height)
        self.Title = "Test WebDialog"

if __name__ == "__main__":
    dialog = WebDialog(width=500, height=500)
    dialog.Show()

Test 5 (Something happens at least)

So if you call the WebDialog class without passing the args the class will at least initalize.

import Eto.Forms as forms
import Eto.Drawing as drawing

class WebDialog(forms.Form):
    def __init__(self, width=400, height=200):
        self.Size = drawing.Size(width, height)
        self.Title = "Test WebDialog"

if __name__ == "__main__":
    dialog = WebDialog() # Called without args works
    dialog.Show()

Other Things I’ve tried… with no luck.

  • Reseting the Script Engine
  • Explicitly delcaring the constructor like so:
class WebDialog(forms.Form):
    def __new__(cls, width=400, height=200):
        return super(WebDialog, cls).__new__(cls)

    def __init__(self, width=400, height=200):
        super(WebDialog, self).__init__()

Any help would be super awesome. :star:

Hi bob,

When you inherit from a .NET class in IronPython the constructor is derived before the derived class constructor.

import Eto.Forms as forms
import Eto.Drawing as drawing

class WebDialog(forms.Form):
    @classmethod
    def create(cls, width=400, height=200):
        instance = cls()
        instance.initialize(width, height)
        return instance

    def initialize(self, width, height):
        self.Size = drawing.Size(width, height)
        self.Title = "Test WebDialog"

if __name__ == "__main__":
    dialog = WebDialog.create(500, 500)
    dialog.Show()

I hope that helps clarify things a little bit,
Farouk

4 Likes

ahh that makes sense. I’m still learning the quirks of IronPython. I’ll be sure to bookmark this one. Many thanks! :pray:

1 Like

Thanks for sharing this @farouk.serragedine ,

That works great for a .Forms class. I was hoping to do something similar with a .Drawable class but cannot seem to get it working as it appears the Drawable class only accepts 1 additional argument.

Any ideas how we can get past this for a custom Drawable class?

I made a new post here, if you happen to have a moment I would appreciate your insights!

Thank you both!

Glad to see you resolved @michaelvollrath
I have not been very active lately sorry :slight_smile:

1 Like

No worries, thanks for checking!

1 Like