Question

I am trying to create a rectangle tool for a paint program using python. Basically, I would like for the user to be able to click on the canvas and to be able to draw rectangles from that specific point just as any rectangle tool in paint programs. This is the code I have right now. It currently gives me very small cross-like structure. I am not sure what is causing this output and would just like some insight on how the problem can be fixed. Thank you.

if mb[0] == 1 and canvas.collidepoint(mx,my):
    screen.set_clip(canvas)
    if tool == "rectangle":
        screen.blit(copy,(0,0))
        x,y = mouse.get_pos()
        mx,my = mouse.get_pos()
        draw.rect(screen,(c),(x,y,mx-x,my-y),sz)
    screen.set_clip(None)
Was it helpful?

Solution

Instead of grabbing the current position, use mouse events.

  • On mouse down, store start coordinate
  • On mouse up, draw rect at start to current coordinates

You could draw in-progress rectangles with MOUSEMOTION's coordinates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top