class_Drawable

This is the first time I’ve used a class to encapsulate some custom methods. It’s a challenge for me. I failed immediately, I felt like I was going to start losing my hair, and the community was my last hope
my code:

import Eto.Forms as ef
import Eto.Drawing as ed
class Txet_draw(ef.Drawable):
    def __init__(self, Txet):
        self.Width = 100
        self.Height = 100
        self._Txet = Txet
        self._font = ed.Font("Arial", 10)
        self._color = ed.Colors.Black
        self._darw_location = ed.PointF(0,0)
    def txet(self,e):
        e.Graphics.DrawText(self._font,self._color,self._darw_location,self._Txet)
form = ef.Form()
form.Width = 300
form.Height = 300
#---------
class_font = Txet_draw(Txet = "123")
a_class_font =class_font.txet()
#---------
Layout_0 = ef.DynamicLayout()
Layout_0.AddRow(a_class_font)
form.Content = Layout_0
form.Show()

Report an error:

Rhino.Runtime.Code.Execution.ExecuteException: Object reference not set to an instance of an object.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Eto.Forms.Control.set_Width(Int32 value) in D:\BuildAgent\work\dujour\src4\DotNetSDK\Eto\src\Eto\Forms\Controls\Control.cs:line 895
   at __main__.Eto.Forms.Drawable__Txet_draw._BASEVIRTUAL__set_Width(Int32)
   at InvokeStub_Drawable__Txet_draw._BASEVIRTUAL__set_Width(Object, Object, IntPtr*)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
System.NullReferenceException: Object reference not set to an instance of an object.
   at Eto.Forms.Control.set_Width(Int32 value) in D:\BuildAgent\work\dujour\src4\DotNetSDK\Eto\src\Eto\Forms\Controls\Control.cs:line 895
   at __main__.Eto.Forms.Drawable__Txet_draw._BASEVIRTUAL__set_Width(Int32)
   at InvokeStub_Drawable__Txet_draw._BASEVIRTUAL__set_Width(Object, Object, IntPtr*)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

You are missing super().__init__() at the begin of your __init__. The _ScriptEditor will tell you so

Also, you should rename your txet method to OnPaint. And pass class_font directly to AddRow

#! python 3
import Eto.Forms as ef
import Eto.Drawing as ed

class Txet_draw(ef.Drawable):
    def __init__(self, Txet):
        super().__init__()
        self.Width = 100
        self.Height = 100
        self._Txet = Txet
        self._font = ed.Font("Arial", 10)
        self._color = ed.Colors.Black
        self._darw_location = ed.PointF(0,0)
    def OnPaint(self,e):
        e.Graphics.DrawText(self._font,self._color,self._darw_location,self._Txet)
form = ef.Form()
form.Width = 300
form.Height = 300
#---------
class_font = Txet_draw(Txet = "123")

#---------
Layout_0 = ef.DynamicLayout()
Layout_0.AddRow(class_font)
form.Content = Layout_0
form.Show()
2 Likes

Sir, you are my savior. Because of your help, I will continue to swim in the ocean of eto

1 Like

Sir I defined an “OnMouseDown” event function in this class, I want to add a method to close the window such as “ef.Form.Close()”, I tried to use this but it didn’t work, I want to make a more general method to close the window directly inside the mouse event

    def OnMouseDown(self, e):
        #Forms_close()
        #ef.Form.Close()
        rs.Command(str(self._Txet),echo=True)

@tom33

Try using self.ParentWindow.Close(). ef.Form is the class, not the instance of the form/window that the control is on.

Hope this helps!

3 Likes

It was very useful and I learned something new

1 Like