0

I wanna click once to set the origin of the line then move the mouse and click the second time to draw the line from the previously set origin to the current location of the mouse pointer

I tried setpos(x,y) and then goto(x,y) but that didnt work Can you help me

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()
  • It will help if you share your code and elaborate what exactly didn't work. – bereal Nov 23 '19 at 17:32
  • just added the code for you to see – greenpassion Nov 23 '19 at 18:48
  • Hello, Benedict. Please post also the output when you try to run this code. Also describe what you're expecting to happen and how precisely that is not happening. Voting to leave the question open, pending Benedict's output. – Brian Dant Nov 23 '19 at 21:19

1 Answers1

0

There are two problems. First, you need to keep the current state: whether you're currently drawing a line. Second, as explained in this answer, you need to call onclick on the Screen object, or turtle.onscreenclick. With these two fixes the program will look like:

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()
bereal
  • 32,519
  • 6
  • 58
  • 104