0

Currently, I have a function that detects and reports the location of mouse clicks on a canvas. What I need it to do is report the first click then draw a rectangle to the mouse until it detects a second click. Below is the code for the canvas item :

Canvas = Canvas(FrameFractal,height = 300,width = 496)
Canvas.pack()
StartImage = ImageTk.PhotoImage(master=FrameFractal , file = "Program files\Show.png")
ImageContainer = Canvas.create_image(250,150, image=StartImage)
Canvas.tag_bind(ImageContainer, '<Button-1>',Zoom)


def Zoom(event):
   print(f"Canvas clicked at {event.x, event.y}")

If it helps, the Canvas is the grey item behind the Fractal Image (StartImage): Main Program

Any help would be appreciated !!

  • Does this answer your question? https://stackoverflow.com/questions/12837575/draw-rectangle-on-mouse-click-python/12837987#12837987 – Bryan Oakley Dec 10 '21 at 18:56

1 Answers1

0

You could use the

canvas.create_rectangle(x1, y1, x2, y2, **kwargs)

the coordinates passed in should be the ones you get from the event such as

canvas.create_rectangle(event.x-size, event.y-size, event.x+size, event.y+size)
  • I'm trying to use that at the moment, just struggling with getting the rectangle to draw from the original click to the mouse until it detects a second click (To which the rectangle stops "following" the mouse) – Shaun Nicholls Dec 10 '21 at 17:16
  • You might want to create a thread, it will allow you to run a code that generate the rectangle over your mouse in parrallel from your normal code. In the thread you might want to : - remove the last rectangle - draw a new one from pointer position (I think you can get the position with a function) - make the thread sleep for 50 ms. – Léo Lassout Dec 11 '21 at 21:43