title: Tkinter: Draw rectangle using a mouse
body: <p>Please, help me to resolve this problem.</p>
  
  <p>I want to allow the user to draw a random rectangle around a specific region of interest in a picture using the mouse ( by clicking the right or left button of the mouse until he releases it).</p>
  
  <p>I deal with large images (images larger than the resolution of my screen, such as this <a href="http://en.wikipedia.org/wiki/Rose#/media/File:Rose_Amber_Flush_20070601.jpg" rel="noreferrer">one</a>), so the user needs to scroll the window in order to be able to see the picture fully.</p>
  
  <p>Here is the code I tried just to display a large picture, but I have no idea on how to allow the user to draw using his mouse a rectangle over an object (say a person in a picture):</p>
  
  <pre><code>from Tkinter import *
  import Image,ImageTk
  
  root=Tk()
  canv=Canvas(root,relief=SUNKEN)
  
  sbarv=Scrollbar(root,orient=VERTICAL)
  sbarh=Scrollbar(root,orien=HORIZONTAL)
  
  sbarv.config(command=canv.yview)
  sbarh.config(command=canv.xview)
  
  canv.config(yscrollcommand=sbarv.set)
  canv.config(xscrollcommand=sbarh.set)
  
  canv.grid(row=0,column=0,sticky=N+S+E+W)
  sbarv.grid(row=0,column=1,sticky=N+S)
  
  sbarh.grid(row=1,column=0,sticky=E+W)
  
  
  im=Image.open("image.jpg")
  width,height=im.size
  canv.config(scrollregion=(0,0,width,height))
  im2=ImageTk.PhotoImage(im)
  imgtag=canv.create_image(0,0,anchor="nw",image=im2)
  
  root.mainloop()
  </code></pre>
  
  <p><strong>EDIT 1:</strong></p>
  
  <ul>
  <li><p>The rectangle must not be filled. I mean I want to draw only its 4 lines (segments) but it must be empty inside, I want to draw only its contours in one pixel width.  </p></li>
  <li><p>I also want to draw as the cursor is moving (dragging) not after button release. </p></li>
  <li><p>Also, note that the rectangle to draw may be a long one, I mean the vertical scroll-bar will need to move down to be able to delimitate the whole object of interest (let's say it is a person)</p></li>
  </ul>
  
  <p>Any help will be highly appreciated.</p>
  
  <p>Thank you very much in advance</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Following the link given to me above, I coded this. My problem is that the <strong>scroll-bars do not appear</strong>. May be someone could tell me why ?</p>
  
  <p>Note that in this code, I resolved the first and second problems highlighted in <strong>EDIT 1</strong>:</p>
  
  <pre><code>import PIL.Image
  import Image
  import ImageTk
  from Tkinter import *    
  
  
  class ExampleApp(Frame):
      def __init__(self,master):
          Frame.__init__(self,master=None)
          self.x = self.y = 0
          self.canvas = Canvas(master,  cursor="cross")
  
          self.sbarv=Scrollbar(self,orient=VERTICAL)
          self.sbarh=Scrollbar(self,orient=HORIZONTAL)
          self.sbarv.config(command=self.canvas.yview)
          self.sbarh.config(command=self.canvas.xview)
  
          self.canvas.config(yscrollcommand=self.sbarv.set)
          self.canvas.config(xscrollcommand=self.sbarh.set)
  
          self.canvas.grid(row=0,column=0,sticky=N+S+E+W)
          self.sbarv.grid(row=0,column=1,stick=N+S)
          self.sbarh.grid(row=1,column=0,sticky=E+W)
  
          self.canvas.bind("&lt;ButtonPress-1&gt;", self.on_button_press)
          self.canvas.bind("&lt;B1-Motion&gt;", self.on_move_press)
          self.canvas.bind("&lt;ButtonRelease-1&gt;", self.on_button_release)
  
          self.rect = None
  
          self.start_x = None
          self.start_y = None
  
  
          self.im = PIL.Image.open("logo.png")
          self.wazil,self.lard=self.im.size
          self.canvas.config(scrollregion=(0,0,self.wazil,self.lard))
          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="")
  
      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__":
      root=Tk()
      app = ExampleApp(root)
      root.mainloop()
  </code></pre>