Python turtle : 创建重做函数
Python turtle : Create a redo function
我知道如何使用 turtle.undo()
撤消 python 海龟中的绘图步骤。但是我怎样才能做一个重做功能呢?
from tkinter import *
...#Just some other things
def undoStep():
turtle.undo()
def redoStep():
#What to put here
root.mainloop()
要创建 redo
函数,您需要跟踪每个操作,例如在列表 actions
中。您还需要一个变量 i
来告诉您您在该列表中的位置,并且每次调用 undoStep
时,将 i
减一。然后 redoStep
必须执行操作 actions[i]
。这是代码:
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
我知道如何使用 turtle.undo()
撤消 python 海龟中的绘图步骤。但是我怎样才能做一个重做功能呢?
from tkinter import *
...#Just some other things
def undoStep():
turtle.undo()
def redoStep():
#What to put here
root.mainloop()
要创建 redo
函数,您需要跟踪每个操作,例如在列表 actions
中。您还需要一个变量 i
来告诉您您在该列表中的位置,并且每次调用 undoStep
时,将 i
减一。然后 redoStep
必须执行操作 actions[i]
。这是代码:
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