Python turtle : Create a redo function

Question:

I know how to undo a drawing step in python turtle with turtle.undo(). But how can I make a Redo function ?

from tkinter import *
...#Just some other things

def undoStep():
    turtle.undo()

def redoStep():
    #What to put here


root.mainloop()
Asked By: Hung Truong

||

Answers:

To make a redo function, you need to keep track of the each action, in a list actions for example. You will also need a variable i that tells you where you are in that list and each time you call undoStep, decrease i by one. Then redoStep has to perform the action actions[i]. Here is the code:

import turtle

actions = []
i = 0

def doStep(function, *args):
    global i
    actions.append((function, *args))
    i += 1
    function(*args)


def undoStep():
    global i
    if i > 0:
        i -= 1
        turtle.undo()

def redoStep():
    global i
    if i >= 0 and i < len(actions):
        function, *args = actions[i]
        function(*args)
        i += 1
Answered By: j_4321