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
            self.ButtonBounds=rec1

        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)
                button.Dispose()

        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)

            else:
                pass

    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
            self.Params.Input.Add(p)

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

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

        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
            self.m_attributes=Attributes_Custom(self) 

        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:

2 Likes

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.

Excelent work , thank you

Check this, you need to create new empty script than paste the code and fix it

image

ghpython button.gh (7.0 KB)

1 Like

Hi @shubhamjain107 , do you have an idea how to use the click button to get True/False output instead of the messagebox?