# how to response vtkButtonWidget?such as LeftButtonPressEvent

**URL:** <https://discourse.slicer.org/t/how-to-response-vtkbuttonwidget-such-as-leftbuttonpressevent/8937>\
**Category:** Support\
**Created:** [October 29, 2019, 11:47am UTC](https://discourse.slicer.org/t/how-to-response-vtkbuttonwidget-such-as-leftbuttonpressevent/8937 "2019-10-29T11:47:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pingdan](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@pingdan](https://discourse.slicer.org/u/pingdan)\
**Post date:** [October 29, 2019, 11:47am UTC](https://discourse.slicer.org/t/how-to-response-vtkbuttonwidget-such-as-leftbuttonpressevent/8937/1 "2019-10-29T11:47:32Z")

</div>

I created a vtkButtonWidget and want to do some work when I click the buttonwidget with the left button of mouse, so I add a observer to the buttonwidget, but when I click ,it has no action. what is wrong with the codes?

class vtkButtonCallback : public vtkCommand  
{  
public:  
static vtkButtonCallback \*New()  
{  
return new vtkButtonCallback;  
}  
virtual void Execute(vtkObject _caller, unsigned long, void_)  
{  
if (ev == vtkCommand::LeftButtonPressEvent)  
{  
return;  
}  
}  
};

```
// Create the widget and its representation
vtkSmartPointer<vtkTexturedButtonRepresentation2D> buttonRepresentation =
	vtkSmartPointer<vtkTexturedButtonRepresentation2D>::New();
buttonRepresentation->SetNumberOfStates(1);
buttonRepresentation->SetButtonTexture(0, image1);

vtkSmartPointer<vtkButtonWidget> buttonWidget =
	vtkSmartPointer<vtkButtonWidget>::New();
buttonWidget->SetInteractor(renderWindowInteractor);
buttonWidget->SetRepresentation(buttonRepresentation);

vtkButtonCallback *myBtnCallback = vtkButtonCallback::New();
buttonWidget->AddObserver(vtkCommand::LeftButtonPressEvent, myBtnCallback);
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 29, 2019, 12:20pm UTC](https://discourse.slicer.org/t/how-to-response-vtkbuttonwidget-such-as-leftbuttonpressevent/8937/2 "2019-10-29T12:20:11Z")

</div>

We have tried to use VTK widgets in Slicer but there they had so many problems and limitations that we gave up on them.

I would recommend to add custom buttons in the view controller bars:

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Customize\_widgets\_in\_view\_controller\_bars](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Customize_widgets_in_view_controller_bars)
