Change slice offset automatically

Hi, all
I saw a script here, 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.

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')

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 in Screen Capture module.

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