ユーザーがキーボードで押したタートル キャンバスに任意の文字を描画するタートル描画プログラムを作成しました。ユーザーが呼び出した最後の描画を元に戻すための元に戻す機能を既に実装しています (以下を参照) 。おそらく私の現在の元に戻す機能に基づいて、これを行うための最もpythonicな方法に関するヒントやコツを教えてもらえますか? 私はこれについて多くのことをグーグルで検索しましたが役に立たなかったので、この問題に関する助けは大歓迎です。
私の元に戻す機能:
def Clear():
clear()
speed(0)
tracer(0,0)
def undoHandler():
if len(function) > 0:
undoHandler.handling = True
if not hasattr(undoHandler, "counter"):
undoHandler.counter = 0
undoHandler.counter += 1
Clear()
function.pop()
penup()
try:
goto(o,p)
print("Gone to")
except:
goto(-200, 100)
pendown()
# "function" is a deque I created with the letter functions appended to it
# Items created based on a Points class that also stores all the attributes including the width, height, color, etc. of each letter.
# Items from a queue I created for the letter functions. The following executes each item from the deque.
try:
for i in function:
k = i.getXY()
penup()
goto(k)
pendown()
hk = i.getletterheight()
global letter_height
letter_height = hk
rk = i.getletterwidth()
global letter_width
letter_width = rk
hw = i.getwidth()
width(hw)
op = i.getcolor()
try:
color(op)
except:
for g in colors:
cp = g.getcolor2()
colormode(255)
color(cp)
j = i.getfunction()
j()
except:
pass
update()
編集:混乱を避けるために、「やり直し」でやりたいことは、キャンバスをクリアし、「やり直し」を呼び出すボタンが押されるたびに、元に戻したポイントを過ぎて1つの関数ですべてを再描画することです。たとえば、ユーザーがキャンバスに「HELLO」を描画し、ユーザーが文字「H」まで元に戻した場合、やり直しを 1 回押すと、タートルは「H(ユーザーが選択した新しい文字)L」を再描画する必要があります。 2 回目の呼び出しでは、タートルは「H(ユーザーが選択した新しい文字)LL」を描画する必要があります。また、元に戻した文字を、ユーザーが置き換えた文字に変更できる必要があります (したがって、「やり直し」)。たとえば、ユーザーが "HELLO" の "H" までを元に戻し、ユーザーが "E" を "A" に置き換えた場合、redo が呼び出されると、