# Update UI widget after programmatically set values in volume node

**URL:** <https://discourse.slicer.org/t/update-ui-widget-after-programmatically-set-values-in-volume-node/28882>\
**Category:** Development\
**Tags:** python\
**Created:** [April 13, 2023, 12:12pm UTC](https://discourse.slicer.org/t/update-ui-widget-after-programmatically-set-values-in-volume-node/28882 "2023-04-13T12:12:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonJones](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@RonJones](https://discourse.slicer.org/u/RonJones)\
**Post date:** [April 13, 2023, 12:12pm UTC](https://discourse.slicer.org/t/update-ui-widget-after-programmatically-set-values-in-volume-node/28882/1 "2023-04-13T12:12:20Z")

</div>

I adapted the code from the activate hanging protocol example to set the color window/levels. ([Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#activate-hanging-protocol-by-keyboard-shortcut)))

The volume updates correctly and shows the correct color levels, but the UI widgets are not updated. Specifically the window/level widgets in the Volume UI.

This code sets the values in the volume

```auto
petNode = slicer.util.getNodesByClass('vtkMRMLScalarVolumeNode'):
petColor = slicer.mrmlScene.GetFirstNodeByName('PET-Heat')
petNode.GetVolumeDisplayNode().SetAndObserveColorNodeID(petColor.GetID())
petNode.GetVolumeDisplayNode().SetWindowLevelMinMax(0, 20)

```

How do the UI widgets get notified of the change?

thanks.

---

<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:** [April 13, 2023, 12:14pm UTC](https://discourse.slicer.org/t/update-ui-widget-after-programmatically-set-values-in-volume-node/28882/2 "2023-04-13T12:14:24Z")

</div>

The GUI updates automatically when the MRML nodes are modified. Volumes module probably showed a different volume than what you modified. Or maybe you modified the volume rendering display node. This code snippet changes scalar volume display nodes of all loaded volumes:

```python
petColor = slicer.mrmlScene.GetFirstNodeByName('PET-Heat')
petNodes = slicer.util.getNodesByClass('vtkMRMLScalarVolumeNode')
for petNode in petNodes:
    displayNode = petNode.GetScalarVolumeDisplayNode()
    displayNode.SetAndObserveColorNodeID(petColor.GetID())
    displayNode.SetWindowLevelMinMax(0, 20)

```

---

<div class="post-metadata">

**Author:** ![RonJones](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@RonJones](https://discourse.slicer.org/u/RonJones)\
**Post date:** [April 14, 2023, 4:24am UTC](https://discourse.slicer.org/t/update-ui-widget-after-programmatically-set-values-in-volume-node/28882/3 "2023-04-14T04:24:10Z")

</div>

Thanks, much appreciated. The UI widgets were being updated.

I worked out I needed to explicitly turn off auto window level before setting the window/level values so they weren’t immediately overridden in the UI widget.

```auto
displayNode = petNode.GetScalarVolumeDisplayNode()
displayNode.SetAndObserveColorNodeID(petColor.GetID())
displayNode.AutoWindowLevelOff()
displayNode.SetWindowLevelMinMax(0, 20)

```

It works now. Thanks.
