【问题标题】:Python turtle : Create a redo functionPython turtle:创建一个重做函数
【发布时间】:2016-07-17 10:41:53
【问题描述】:
我知道如何使用turtle.undo() 撤消 python turtle 中的绘图步骤。但是我怎样才能做一个重做功能呢?
from tkinter import *
...#Just some other things
def undoStep():
turtle.undo()
def redoStep():
#What to put here
root.mainloop()
【问题讨论】:
标签:
python
tkinter
turtle-graphics
undo-redo
【解决方案1】:
要创建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