# Show scalar bar programmatically

**URL:** <https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842>\
**Category:** Support\
**Created:** [January 16, 2020, 9:03pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842 "2020-01-16T21:03:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [January 16, 2020, 9:03pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/1 "2020-01-16T21:03:24Z")

</div>

Hi all,

I’d like to show the scalar bar like when I do DataProbe -\> ScalarBar -\> Enable -\> Foreground. Do you know how I can do this? I’ve always had a hard time accessing the DataProbe stuff from Python.

Cheers,  
Fernando

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 16, 2020, 9:30pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/2 "2020-01-16T21:30:44Z")

</div>

Hi @Fernando - this should work for you (although we might want to think of a better API).

```auto
qt.QSettings().setValue('DataProbe/sliceViewAnnotations.scalarBarEnabled', True)
import DataProbeLib
DataProbeLib.SliceAnnotations().updateSliceViewFromGUI()

```

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [January 16, 2020, 9:45pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/3 "2020-01-16T21:45:09Z")

</div>

Thanks, Steve. That worked great to enable the scalar bar. Do you know how I can specifically show the foreground one?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 16, 2020, 10:04pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/4 "2020-01-16T22:04:27Z")

</div>

It’ll be a similar pattern - have a look at the various options [in the code](https://github.com/Slicer/Slicer/blob/25b804e35bf5ee27e14a318cd44680850825b33b/Modules/Scripted/DataProbe/DataProbeLib/SliceViewAnnotations.py#L222)…

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [January 16, 2020, 10:18pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/5 "2020-01-16T22:18:59Z")

</div>

I did! But the choice between background and foreground doesn’t seem to be part of those settings…

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 17, 2020, 1:05am UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/6 "2020-01-17T01:05:31Z")

</div>

Right - looks like that setting is not persisted in the settings. Maybe you want to add code to persist it to be consistent. Probably the best thing to do would be to expose a proper logic API for these in the DataProbe code.

---

<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:** [January 17, 2020, 1:29pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/7 "2020-01-17T13:29:28Z")

</div>

Data probe (that includes scalar widget management) would need to be completely rewritten. We’ve been planning to do this but there have been always higher-priority tasks.  
@Fernando If you can achieve what you need with small changes then go on, but if you have some more time then it would be great if you could help with redesign/reimplementation of the feature.

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [January 17, 2020, 1:43pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/8 "2020-01-17T13:43:57Z")

</div>

I’ll let you know if I find a solution. @lassoan, that code has always been a bit obscure to me, but I’m happy to help if I can.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 17, 2020, 7:22pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/9 "2020-01-17T19:22:21Z")

</div>

> [@pieper](#):
>
> qt.QSettings().setValue(‘DataProbe/sliceViewAnnotations.scalarBarEnabled’, True) import DataProbeLib DataProbeLib.SliceAnnotations().updateSliceViewFromGUI()

Update: the argument to `setValue` here needs to be an int not a boolean in order to be restored correctly:

```auto
qt.QSettings().setValue('DataProbe/sliceViewAnnotations.scalarBarEnabled', 1)
import DataProbeLib
DataProbeLib.SliceAnnotations().updateSliceViewFromGUI()

```

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [March 31, 2020, 1:24pm UTC](https://discourse.slicer.org/t/show-scalar-bar-programmatically/9842/10 "2020-03-31T13:24:40Z")

</div>

PR here: [https://github.com/Slicer/Slicer/pull/4789](https://github.com/Slicer/Slicer/pull/4789)
