drawable_Mouse_events

I’m trying to use the mouse event to change the color of the drawable image, but I seem to run into problems on “drawable.mousemove += move” it’s wrong, “drawable.MouseDown += down” is fine, but I seem to have hit a dead end

#! python 3

import Rhino
import Eto
import Eto.Forms as ef
import Eto.Drawing as ed
import rhinoscriptsyntax as rs


class SampleTransparentEtoForm(Eto.Forms.Form):
    def __init__(self):
        super().__init__()
        self.AutoSize = False
        self.Resizable = True
        self.TopMost = True
        self.ShowActivated = False
        self.Size = Eto.Drawing.Size(300, 300)
        self.Padding = Eto.Drawing.Padding(80)
        self.MovableByWindowBackground = True
        self.Location = Eto.Drawing.Point(100, 100)

        self.Button = Eto.Forms.Button()
        self.Button.Text = "Close"
        self.Button.Click += self.OnButtonClick

        drawable = ef.Drawable()
        drawable.Width = 40
        drawable.Height = 40
        def move(sender, e):
            cor = True
        def draw(sender, e):
            rect = ed.RectangleF(0, 0, 40, 40)
            if cor:
                e.Graphics.FillEllipse(ed.Colors.Red, rect)
            e.Graphics.FillEllipse(ed.Colors.Green, rect)
        def down(sender, e):
            rs.Command("down_on")
        drawable.Paint += draw
        drawable.MouseDown += down 
        drawable.MouseMove += move #here

        self.layout = Eto.Forms.DynamicLayout()
        self.layout.AddRow(self.Button, None, drawable)

        self.Content = self.layout

    def OnButtonClick(self, sender, e):
        self.Close()

def EstablishForm():
    form = SampleTransparentEtoForm()
    form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
    form.Show()

if __name__ == "__main__":
    EstablishForm()

我是否可以像"OnButtonClick"一样在构造函数外定义事件函数

Hey @tom33,

You need to tell the Drawable that it needs repainting after changing state (e.g. your cor variable). You can do this by calling drawable.Invalidate(), which will ensure its Paint event is triggered again.

That being said, even if cor is true there you’ll still never see the red color as you are painting green right afterwards. Also, cor will never be set to false so once it turns red it’ll stay red.

Curtis.

1 Like

Thank you for your guidance, because of you, I feel Eto’s world is so beautiful

1 Like