我正在用Python制作一个绘画应用程序.它具有使用点击和拖动制作形状和绘制的功能.
我一直在try 在我的应用程序中实现一个撤销按钮.撤销形状效果良好,但撤销点击和拖动线效果不佳.
我的功能:
def undo ():
if la[len(la)-1] =='s' :
turtle.undo()
del(la[len(la)-1])
while(turtle.isdown()==True) :
turtle.undo()
turtle.undo()
turtle.undo()
if la[len(la)-1]=='d' :
turtle.undo()
del(la[len(la)-1])
while turtle.xcor()!=xp[len(xp)-1] and turtle.ycor()!=yp[len(yp)-1] :
turtle.undo()
del(xp[len(xp)-1])
del(yp[len(yp)-1])
la是一个数组,它记住最后一个动作是画线还是画形状.
如果是形状(' s '),它会在笔向下时撤销(由于形状是使用 turtle 动作制成的,因此笔不会在这些动作之间抬起).
如果是一条线(' d '),则应该撤销,直到 turtle 的坐标与线开头的坐标相同(记住在XP和yp中).
嗯,有时它会撤销比它应该撤销的更多,有时它根本不会撤销.问题是,大多数时候它都工作得很完美,所以我不知道是什么导致了这些问题.最常见的情况是,当线之前有一个形状时,它也会取消部分形状.当然,如果你多次越过乞讨线,它就会停止撤销,但可以通过计算你越过它的次数来修复这一点.
编辑:
最低工作计划:
from tkinter import *
from functools import partial
from turtle import TurtleScreen, RawTurtle, Shape
menu = Tk()
frame=Frame(menu, bd=1, bg='#d1c7c7')
frame.grid(row=1, column=6, rowspan=26 )
canvas=Canvas(frame,width=1000, height=700)
canvas.grid(column=6,row=1, rowspan=26)
size=5
la=['n']
xp=[]
yp=[]
# turtle actions
def draw(x, y):
turtle.ondrag(None)
turtle.down()
turtle.goto(x, y)
turtle.up()
screen.update()
turtle.ondrag(draw)
def move(x, y):
global xp
global yp
global la
xp.append(turtle.xcor())
yp.append(turtle.ycor())
la.append('d')
screen.onscreenclick(None)
turtle.goto(x, y)
screen.onclick(move)
screen.update()
def main():
turtle.shape("circle")
polygon = turtle.get_shapepoly()
fixed_color_turtle = Shape("compound")
fixed_color_turtle.addcomponent(polygon, "", "")
screen.register_shape('fixed', fixed_color_turtle)
turtle.shape("fixed")
turtle.penup()
turtle.pensize(5)
turtle.turtlesize(2000,2000)
turtle.ondrag(draw)
screen.onscreenclick(move)
screen.update()
def setcolor (color) :
turtle.pencolor(color)
turtle.fillcolor(color)
bblack = Button(
menu,
bg='black',
width=10,
command=partial(setcolor,'black')
).grid(column=1, row=2)
bred = Button(
menu,
bg='red',
width=10,
command=partial(setcolor,'red')
).grid(column=1, row=3)
bdraw = Button(
menu,
text='pen',
width=10,
command=main
).grid(column=1, row=4)
def square(x, y):
global la
la.append('s')
turtle.turtlesize(1,1)
screen.onclick(None)
turtle.ondrag(None)
turtle.goto(x-size*8.3, y-size*8.3)
turtle.pendown()
turtle.begin_fill()
for i in range(4):
turtle.forward(size*18)
turtle.left(360 / 4)
turtle.end_fill()
turtle.penup()
possqr()
def possqr():
screen.onclick(square)
bsquare = Button(
menu,
text='square',
width=10,
command=possqr
).grid(column=1, row=5)
def triangle(x, y):
global la
la.append('s')
turtle.turtlesize(1,1)
screen.onclick(None)
turtle.ondrag(None)
turtle.goto(x-size*8.5, y-size*6)
turtle.pendown()
turtle.begin_fill()
for i in range(3):
turtle.forward(size*18)
turtle.left(360 / 3)
turtle.end_fill()
turtle.penup()
postriangle()
def postriangle():
screen.onclick(triangle)
btriangle = Button(
menu,
text='triangle',
width=10,
command=postriangle
).grid(column=1, row=6)
Label(menu, text='COLORS').grid(column=1, row=1)
def undo ():
if la[len(la)-1] =='s' :
turtle.undo()
del(la[len(la)-1])
while(turtle.isdown()==True) :
turtle.undo()
turtle.undo()
turtle.undo()
if la[len(la)-1]=='d' :
turtle.undo()
del(la[len(la)-1])
while turtle.xcor()!=xp[len(xp)-1] and turtle.ycor()!=yp[len(yp)-1] :
turtle.undo()
del(xp[len(xp)-1])
del(yp[len(yp)-1])
bundo = Button(
menu,
text='undo',
width=10,
command=undo
).grid(column=2, row=1)
screen = TurtleScreen(canvas)
screen.tracer(False)
pen_color = 'black'
turtle = RawTurtle(screen)
main()
mainloop()