4

This is with respect to Draw rectangle on mouse click [Python]. I tried out the first solution and it works perfectly. Can someone please tell me, what to do if I want to see the rectangle being drawn and the rectangle gets fixed once the mouse button is released as I can only see the drawn rectangle once the button is released.

Any kind of help will be appreciated.

Community
  • 1
  • 1
Anuradha
  • 1,089
  • 4
  • 18
  • 29

1 Answers1

8

I amended the code from the referenced question. Now it shows the rectangle when mouse is being dragged on the canvas. The canvas displays Lena image.

import tkinter as tk # this is in python 3.4. For python 2.x import Tkinter
from PIL import Image, ImageTk



class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.x = self.y = 0
        self.canvas = tk.Canvas(self, width=512, height=512, cursor="cross")
        self.canvas.pack(side="top", fill="both", expand=True)
        self.canvas.bind("<ButtonPress-1>", self.on_button_press)
        self.canvas.bind("<B1-Motion>", self.on_move_press)
        self.canvas.bind("<ButtonRelease-1>", self.on_button_release)

        self.rect = None

        self.start_x = None
        self.start_y = None


        self._draw_image()


    def _draw_image(self):
         self.im = Image.open('./resource/lena.jpg')
         self.tk_im = ImageTk.PhotoImage(self.im)
         self.canvas.create_image(0,0,anchor="nw",image=self.tk_im)



    def on_button_press(self, event):
        # save mouse drag start position
        self.start_x = event.x
        self.start_y = event.y

        # create rectangle if not yet exist
        #if not self.rect:
        self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill="black")

    def on_move_press(self, event):
        curX, curY = (event.x, event.y)

        # expand rectangle as you drag the mouse
        self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)



    def on_button_release(self, event):
        pass


if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()

enter image description here

The black square is drawn on top of the image. Hope this helps.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thanks a lot, it worked . Can I keep an image instead of a canvas in the background ? – Anuradha Jun 10 '14 at 09:10
  • @Anuradha What do you mean by keeping the image? If you want to draw multiple rectangles, just remove 'if not self.rect:', so that it creates rectangles for each mouse press. – Marcin Jun 10 '14 at 09:12
  • no, what I need is drawing a single rectangle on a PIL image. I am not very familiar with this class kind of codes, so facing a bit difficulty in understanding – Anuradha Jun 10 '14 at 09:15
  • Is there any way it can work without the class kind of declaration like self? – Anuradha Jun 11 '14 at 11:55
  • Hello, I used your answer and modified it a little bit. I have an issue [here](http://stackoverflow.com/questions/29789554/tkinter-draw-rectangle-using-a-mouse) if you could help me. Thank you in advance –  Apr 22 '15 at 11:46