# Adding Button with GhPython?

**URL:** <https://discourse.mcneel.com/t/adding-button-with-ghpython/111359>\
**Category:** Grasshopper Developer\
**Tags:** ghpython, rhinocommon\
**Created:** [October 31, 2020, 9:37pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359 "2020-10-31T21:37:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [October 31, 2020, 9:37pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/1 "2020-10-31T21:37:55Z")

</div>

Hello  
Is it possible to add a button with command like undo using Ghpython?

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/4/0/2/402a87af533650a5728aafe24d159f8091e31171.png)

---

<div class="post-metadata">

**Author:** ![pierrec](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/pierrec/32/765934_2.png) [@pierrec](https://discourse.mcneel.com/u/pierrec)\
**Post date:** [November 2, 2020, 1:41pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/2 "2020-11-02T13:41:25Z")

</div>

Hi Seghier,

The old GH forums have some information on how to add buttons to the Grasshopper toolbar:

> **[Add Buttons to Canvas Toolbar](https://www.grasshopper3d.com/forum/topics/add-buttons-to-canvas-toolbar)**
>
> Is it possible to add buttons to the Canvas Toolbar?
> Thanks.

I did not try to do much with it but this code adds a button to the toolbar in Python:

```python
import Grasshopper

toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]
toolbar.Items.Add("Hey I just got inside the toolbar")

```

The docs for the ToolStrip item that you will add are there:

> **[GH\_DocumentObject.Menu\_AppendItem Method](https://developer.rhino3d.com/api/grasshopper/html/Overload_Grasshopper_Kernel_GH_DocumentObject_Menu_AppendItem.htm)**

Finally, undo methods and undo history are available through RhinoCommon for both Rhino (`Rhino.RhinoDoc.ActiveDoc.Undo()`) and Grasshopper (`Grasshopper.Instances.ActiveCanvas.Document.Undo()`).

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [November 2, 2020, 2:21pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/3 "2020-11-02T14:21:17Z")

</div>

Thank you @pierrec , i will check it and give it a try 🙂

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [November 2, 2020, 2:41pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/4 "2020-11-02T14:41:23Z")

</div>

> [@pierrec](#):
>
> `toolbar.Items.Add("Hey I just got inside the toolbar")`

Work fine, how to remove it again?

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/2/2/9/229f8f841f5a78d5682d833ea91b40810eb54054.png)

* * *

Solved : Got it 😃

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/7/b/7/7b71ce7907a97de1eb4f5413e63d75f7e0150fe6.png)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [November 2, 2020, 3:16pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/5 "2020-11-02T15:16:01Z")

</div>

This is the first step , but it need more work to add button like named view without duplicate.  
how we can assign function like undo redo to tho button and how we can assign named view?

[add remove button.gh](https://discourse.mcneel.com/uploads/short-url/eJNhIVBcBhCMPWdUdtTr4CEanET.gh) (4.8 KB)

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/c/2/7/c27c1e0b6d54ac36d932425f0ed60b9c16982b00.png)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [November 2, 2020, 5:52pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/6 "2020-11-02T17:52:00Z")

</div>

```cs
private void RunScript(bool run)
{
  if (!run) return;
  var toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1] as ToolStrip;
  toolbar.Items.Add("Undo", null, OnClick);
}
// <Custom additional code> 
private void OnClick(object sender, EventArgs e)
{
  Grasshopper.Instances.ActiveCanvas.Document.Undo();
}
// </Custom additional code> 

```

```python
import Grasshopper
def OnClick(sender, e):
    Grasshopper.Instances.ActiveCanvas.Document.Undo()
if run:
    toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]
    toolbar.Items.Add("Undo", None, OnClick)

```

[Button.gh](https://discourse.mcneel.com/uploads/short-url/yzMfBrUdgATBJFAUJJn826YIUZH.gh) (4.1 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [November 2, 2020, 6:55pm UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/7 "2020-11-02T18:55:54Z")

</div>

Thank you very much @Mahdiyar

---

<div class="post-metadata">

**Author:** ![zhangvip0755](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/zhangvip0755/32/698031_2.png) [@zhangvip0755](https://discourse.mcneel.com/u/zhangvip0755)\
**Post date:** [June 4, 2021, 7:46am UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/8 "2021-06-04T07:46:01Z")

</div>

This is good! But when I restart grasshopper, the button disappears again!

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [June 4, 2021, 9:57am UTC](https://discourse.mcneel.com/t/adding-button-with-ghpython/111359/9 "2021-06-04T09:57:14Z")

</div>

I don’t test it again and it is a script work when you launch it, i think you need make it like a plugin
