Adding a button on canvas using ghPython

How you can add a button on GH_canvas on your component with custom attributes

    from ghpythonlib.componentbase import dotnetcompiledcomponent as component
    import Grasshopper, GhPython
    import System
    from copy import deepcopy as Copy
    import rhinoscriptsyntax as rs
    from System.Windows.Forms import MessageBox
    from System.Windows.Forms import MessageBoxButtons
    class Attributes_Custom(Grasshopper.Kernel.Attributes.GH_ComponentAttributes): 

        def Layout(self): 
            super(Attributes_Custom, self).Layout() 

            rec0 = Grasshopper.Kernel.GH_Convert.ToRectangle(self.Bounds) 
            rec0.Height += 20

            rec1 = Copy(rec0)
            rec1.Y = rec1.Bottom - 20 
            rec1.Height = 20 
            rec1.Inflate(-2, -2)

            self.Bounds = rec0

        def Render(self,canvas, graphics, channel): 
            super(Attributes_Custom, self).Render(canvas, graphics, channel)
            if channel == Grasshopper.GUI.Canvas.GH_CanvasChannel.Objects:
             button = Grasshopper.GUI.Canvas.GH_Capsule.CreateTextCapsule(self.ButtonBounds, self.ButtonBounds, Grasshopper.GUI.Canvas.GH_Palette.Blue, "Click Me", 2, 0)
                button.Render(graphics, self.Selected, self.Owner.Locked, False)

        def RespondToMouseDown(self,sender,e):
            if e.Button == System.Windows.Forms.MouseButtons.Left:
                rec = self.ButtonBounds

                if rec.Contains(e.CanvasLocation.X, e.CanvasLocation.Y):
                    MessageBox.Show("The button was clicked", "Button", MessageBoxButtons.OK)

                    return Grasshopper.GUI.Canvas.GH_ObjectResponse.Handled

                return  super(Attributes_Custom, self).RespondToMouseDown(sender,e)


    class MyComponent(component):
        def __new__(cls):
            instance = Grasshopper.Kernel.GH_Component.__new__(cls,
            "NewComp", "NC", """example : adding a component with button""", "Test", "buttonExample")
            return instance

        def get_ComponentGuid(self):
            return System.Guid("05c92e7f-f3bf-4c7e-a836-8d61e67cec2e")

        def SetUpParam(self, p, name, nickname, description):
            p.Name = name
            p.NickName = nickname
            p.Description = description
            p.Optional = True

        def RegisterInputParams(self, pManager):
            p = GhPython.Assemblies.MarshalParam()
            self.SetUpParam(p, "x", "x", "Script variable Python")
            p.Access = Grasshopper.Kernel.GH_ParamAccess.item

            p = GhPython.Assemblies.MarshalParam()
            self.SetUpParam(p, "y", "y", "Script input y.")
            p.Access = Grasshopper.Kernel.GH_ParamAccess.item

        def RegisterOutputParams(self, pManager):
            p = Grasshopper.Kernel.Parameters.Param_GenericObject()
            self.SetUpParam(p, "a", "a", "Script output a.")

        def SolveInstance(self, DA):
            p0 = self.marshal.GetInput(DA, 0)
            p1 = self.marshal.GetInput(DA, 1)
            result = self.RunScript(p0, p1)
            if result is not None:
                self.marshal.SetOutput(result, DA, 0, True)

        def get_Internal_Icon_24x24(self):
            o = None
        return System.Drawing.Bitmap(System.IO.MemoryStream(System.Convert.FromBase64String(o)))

        def CreateAttributes(self): # Override inherited method create attributes

        def RunScript(self, x, y):

            __author__ = "Shubham Jain"
            __version__ = "2021.02.25"

            a = "Hi from Shubham"
            return a 

My First Post on the Forum :upside_down_face:

Hi Shubham, could you explain the remaining steps to make this work? I think you need to compile it, and simply pasting this in a GHPython component won’t work.