【问题标题】:How do I define a starting position and end position and then tell turtle to connect them如何定义起始位置和结束位置,然后告诉海龟连接它们
【发布时间】:2019-11-23 17:23:48
【问题描述】:

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

我尝试了 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()

【问题讨论】:

  • 如果您分享您的代码并详细说明究竟是什么不起作用,这将有所帮助。
  • 刚刚加了代码给你看
  • 你好,本尼迪克特。当您尝试运行此代码时,请同时发布输出。还要描述您期望发生的事情以及没有发生的准确程度。投票让问题悬而未决,等待本尼迪克特的输出。

标签: python python-3.x turtle-graphics


【解决方案1】:

有两个问题。首先,您需要保持当前状态:您当前是否正在绘制一条线。其次,正如in this answer 解释的那样,您需要在Screen 对象上调用onclick,或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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-08-20
    相关资源
    最近更新 更多