如何定义起始位置和结束位置,然后告诉turtle连接它们

2024-07-08 10:01:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想单击一次来设置线的原点,然后移动鼠标,再单击第二次来绘制线,从先前设置的原点到鼠标指针的当前位置

我试过setpos(x,y)然后goto(x,y),但没用 你能帮我吗

import turtle
beni=turtle.Screen()
beni.setup(900,700)
t=turtle.Turtle()



def freehandmode(x, y):
    t.ondrag(None)
    t.goto(x, y)
    t.ondrag(freehandmode)

t.ondrag(freehandmode)


def linemode(x, y):
    t.setposition(x,y)
    t.goto(x, y)


turtle.mainloop()

Tags: importdefsetup绘制鼠标screenturtle指针
1条回答
网友
1楼 · 发布于 2024-07-08 10:01:57

有两个问题。首先,您需要保持当前状态:当前是否正在绘制一条线。其次,如in this answer所述,您需要调用onclick对象上的Screen,或者turtle.onscreenclick。通过这两个修复程序,程序将如下所示:

import turtle
beni = turtle.Screen()
beni.setup(900,700)

class Drawer:
    def __init__(self):
       self.drawing = False

    def click(self, x, y):
        if self.drawing:
            turtle.down()
            turtle.goto(x, y)
            self.drawing = False
        else:
            turtle.up()
            turtle.goto(x, y)
            self.drawing = True

d = Drawer()
beni.onclick(d.click)

turtle.up()
turtle.mainloop()

相关问题 更多 >

    热门问题