# Text overlayed on slice

**URL:** <https://discourse.slicer.org/t/text-overlayed-on-slice/12982>\
**Category:** Development\
**Created:** [August 13, 2020, 6:04pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982 "2020-08-13T18:04:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [August 13, 2020, 6:04pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/1 "2020-08-13T18:04:06Z")

</div>

Hello,  
Is it possible to add Python code to your scripted module that displays custom text on top of the slice view? In particular, I’m interested in displaying the slice position in a user-friendly way. I created an example of what I want to add to my scripted module in MS Paint; it is shown in the image below

 ![red_slice](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/e/3ec0bc43a6be5bae9ebfa1bef3b5f8606ab1a22e.png)

---

<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:** [August 13, 2020, 6:09pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/2 "2020-08-13T18:09:43Z")

</div>

Slice viewers have corner annotations, so you can easily [add text in any corner](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Display_text_in_a_3D_view_or_slice_view). In case you want to add text to any other location, you need to add a VTK text actor (preferably in a displayable manager).

---

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [August 13, 2020, 8:00pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/3 "2020-08-13T20:00:38Z")

</div>

I tried the method in the link and it worked, but the text permanently disappears as soon as you move the slider. What is the easiest way to make sure that the text gets printed to upper right corner again every time the slice slider position is moved?

---

<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:** [August 13, 2020, 8:46pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/4 "2020-08-13T20:46:49Z")

</div>

DataProbe module already uses corner annotations to display text, which erases any previous content. You can [hide slice view annotations](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Hide_slice_view_annotations_.28DataProbe.29), as you can display any information you need anyway.

---

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [August 13, 2020, 9:40pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/5 "2020-08-13T21:40:54Z")

</div>

Thanks for all your help so far.  
Last question: What is the syntax for calling a function every time the user scrolls to a different slice in red slice view using slider or mouse scroll? I want to do something analogous to this:

self.CheckBox.stateChanged.connect(self.checkboxFunc)

Except that it detects a change in axial position viewed in red slice, instead of checkbox being checked or unchecked.

---

<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:** [August 13, 2020, 9:48pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/6 "2020-08-13T21:48:38Z")

</div>

You can get notification of slice position changes using standard VTK observations. See how `AddObserver` is used to observe a slice in [this example](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Measure_angle_between_two_slice_planes).

---

<div class="post-metadata">

**Author:** ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)\
**Post date:** [August 13, 2020, 11:17pm UTC](https://discourse.slicer.org/t/text-overlayed-on-slice/12982/7 "2020-08-13T23:17:15Z")

</div>

Great, thanks. I understand how to do what I need now.
