Python 我如何定义起始位置和结束位置,然后告诉海龟连接它们
我想单击一次以设置线的原点,然后移动鼠标并单击第二次以将线从先前设置的原点绘制到鼠标指针的当前位置 我尝试了setpos(x,y)和goto(x,y),但没有成功 你能帮我吗Python 我如何定义起始位置和结束位置,然后告诉海龟连接它们,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我想单击一次以设置线的原点,然后移动鼠标并单击第二次以将线从先前设置的原点绘制到鼠标指针的当前位置 我尝试了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)
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()
有两个问题。首先,您需要保持当前状态:是否正在绘制直线。其次,如前所述,您需要调用
屏幕上的onclick
对象,或海龟。onscreen单击。通过这两个修复程序,程序将如下所示:
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()
如果你分享你的代码并详细说明哪些代码不起作用,这会有所帮助。只需添加代码供你查看你好,本尼迪克特。当您尝试运行此代码时,请同时发布输出。同时描述你期望发生的事情以及没有发生的准确程度。投票让问题悬而未决,等待本尼迪克特的结果。