# Change slice offset automatically

**URL:** <https://discourse.slicer.org/t/change-slice-offset-automatically/36483>\
**Category:** Development\
**Created:** [May 30, 2024, 2:46am UTC](https://discourse.slicer.org/t/change-slice-offset-automatically/36483 "2024-05-30T02:46:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiaojie\_Guo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/xiaojie_guo/32/67098_2.png) [@Xiaojie\_Guo](https://discourse.slicer.org/u/Xiaojie_Guo)\
**Post date:** [May 30, 2024, 2:46am UTC](https://discourse.slicer.org/t/change-slice-offset-automatically/36483/1 "2024-05-30T02:46:15Z")

</div>

Hi, all  
I saw a script [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#change-slice-offset), which could change the Red slice.

Suppose there is a CT data of 512 slices. I want to scroll from slice 0 to slice 511 automatically, is it possible to implement that using a python script?

Here is a try to move 2 times in Red slice, but the GUI is not updated immediately. I want to know how to update the GUI.

```auto
import time

layoutManager = slicer.app.layoutManager()

red = layoutManager.sliceWidget("Red")
redLogic = red.sliceLogic()

# Print current slice offset position
print(redLogic.GetSliceOffset())

# Change slice position
redLogic.SetSliceOffset(-400)
print('move first')
    
time.sleep(1.0)
    
redLogic.SetSliceOffset(-500)
print('move sceond')

```

---

<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:** [May 30, 2024, 12:51pm UTC](https://discourse.slicer.org/t/change-slice-offset-automatically/36483/2 "2024-05-30T12:51:57Z")

</div>

This looks good, but Slicer is an event-driven application, so you either need to force a redraw (`slicer.util.forceRenderAllViews()`) or pump the event loop (`slicer.app.processEvents()`) to immediately update the display after data modification.

See a complete implementation of [slicing through the scene and capturing each image](https://github.com/Slicer/Slicer/blob/2e6f53f27386dddacbb690956b35396ba47a53a5/Modules/Scripted/ScreenCapture/ScreenCapture.py#L1228-L1256) in Screen Capture module.

---

<div class="post-metadata">

**Author:** ![Xiaojie\_Guo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/xiaojie_guo/32/67098_2.png) [@Xiaojie\_Guo](https://discourse.slicer.org/u/Xiaojie_Guo)\
**Post date:** [May 31, 2024, 1:54am UTC](https://discourse.slicer.org/t/change-slice-offset-automatically/36483/3 "2024-05-31T01:54:30Z")

</div>

The second choice is what I need. Thanks a lot.
