# Add an observer to the SliceNode's scrollbar

**URL:** https://discourse.slicer.org/t/add-an-observer-to-the-slicenodes-scrollbar/34399
**Category:** Support
**Tags:** python
**Created:** [February 19, 2024, 3:40pm UTC](https://discourse.slicer.org/t/add-an-observer-to-the-slicenodes-scrollbar/34399 "2024-02-19T15:40:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![waromero](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/waromero/32/68278_2.png) [@waromero](https://discourse.slicer.org/u/waromero)
#### Post date: [February 19, 2024, 3:40pm UTC](https://discourse.slicer.org/t/add-an-observer-to-the-slicenodes-scrollbar/34399/1 "2024-02-19T15:40:12Z")

</div>

Hello!

I would like to add an observer to the scrollbar events of a given view. I cannot find the right event to add the callback function.

```auto
viewerNode = slicer.util.getNode("vtkMRMLSliceDisplayNode1") # Or should that be the vtkMRMLSliceNode class?
observerID = viewerNode.AddObserver(<WHICH_CLASS__WHICH_METHOD>, onScrollbarChanged)

```

The callback function:

```auto
def onScrollbarChanged(observer,eventID):
    print(<SLICE_INDEX>)

```

Thank you!

---

<div class="post-metadata">

### Author: ![Sam\_Horvath](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sam_horvath/32/3092_2.png) [@Sam\_Horvath](https://discourse.slicer.org/u/Sam_Horvath)
#### Post date: [February 19, 2024, 8:20pm UTC](https://discourse.slicer.org/t/add-an-observer-to-the-slicenodes-scrollbar/34399/2 "2024-02-19T20:20:02Z")

</div>

So, this will give you the qt object the represents the slider for a particular view (the red slice in this case)

`slider = slicer.app.layoutManager().sliceWidget("Red").sliceController().sliceOffsetSlider()`

you can then do something like:  
`slicer.app.layoutManager().sliceWidget("Red").sliceController().sliceOffsetSlider().valueIsChanging.connect(lambda x : print(x))`

---

<div class="post-metadata">

### Author: ![CyprienB](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cyprienb/32/66306_2.png) [@CyprienB](https://discourse.slicer.org/u/CyprienB)
#### Post date: [February 23, 2024, 2:17pm UTC](https://discourse.slicer.org/t/add-an-observer-to-the-slicenodes-scrollbar/34399/3 "2024-02-23T14:17:35Z")

</div>

**Operating system** : Fedora Linux 39  
**Slicer Version** : 5.6.1

Hello,  
The method mentioned by Sam\_Horvath _valueIsChanging_ was not working for. I have added an Observer on the slice Logic and it works fine.

> sliceLogic = slicer.app.layoutManager().sliceWidget(“Red”).sliceLogic()  
> sliceLogic.AddObserver(vtk.vtkCommand.ModifiedEvent, lambda caller, event: print(“test”))

However, I have noticed that this function will be triggered thrice so you should add a criteria to your function.

Cyprien
