title: Draw rectangle on mouse click [Python]
body: <pre><code>def xaxis(event):
     x1, y1 = (event.x - 1), (event.y - 1)
  
  def yaxis(event):
     x2, y2 = (event.x + 1), (event.y + 1)
  
  def create(event):
     w.create_rectangle(x1,y1,x2,y2,fill='Black')
  
  w = Canvas(root, width=canvas_width, height=canvas_height)
  w.config(cursor='cross')
  w.pack(expand=YES, fill=BOTH)
  w.bind("&lt;Button-1&gt;", xaxis)
  w.bind("&lt;ButtonRelease-1&gt;", yaxis)
  w.bind("&lt;ButtonRelease-1&gt;", create)
  </code></pre>
  
  <p>The shell says </p>
  
  <blockquote>
    <p>Exception in Tkinter callback Traceback (most recent call last):<br>
    File
    "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
    line 1410, in <strong>call</strong>
        return self.func(*args)   File "/Users/Leo/Desktop/draw.py", line 22, in create
        w.create_rectangle(x1,y1,x2,y2,fill='Black') NameError: global name 'x1' is not defined</p>
  </blockquote>
  
  <p>and it think the create function is not able to get the coordinates of the other functions... </p>
  
  <p>I did it this way because I need the coordinates later!</p>
  
  <p>I hope you can help me.. ;-)
  Thanks!</p>