title: Get global mouse position and use in python program?
body: <p>As part of a larger project, I am trying to create a snapshot tool that works similar to the Mac OS X snapshot. It should take a first click, a second click, and return an image of the area created by the square.</p>
  
  <p>I have some python functions that take an first point (x, y) and a second point (x, y) and create a snapshot of the square that those points create on the screenshot. The missing piece is getting the mouse locations of the initial click and second click, then passing that data to the python program to create the snapshot. </p>
  
  <p>In other words, the flow of the program should be:</p>
  
  <ol>
  <li>first click (save x, y)</li>
  <li>second click (save x2, y2)</li>
  <li>run snapshot.py using the saved clicked data to return the screenshot</li>
  </ol>
  
  <p>I've only found solutions that can return the position of the pointer within a frame. If it helps, I'm using "import gtk" and "from Xlib import display"</p>
  
  <p>edit: I have tried to use Tkinter to make an invisible frame that covers the whole screen. The idea was to use that invisible frame to get the exact coordinates of two mouse clicks, and then the invisible frame would disappear, pass the coordinates on to the screenshot function, and it would be done. However, the code I've been writing doesn't keep the frame transparent.</p>
  
  <p>edit 2: This code can create a window, make it transparent, size it to the screen, then return the mouse coordinates on that window. I can use this to simply return the mouse coordinates on two clicks, then remove the window and send those coordinates to the snapshot code. When I run the below code line-by-line in the python shell, it works perfectly. However, whenever I run the code as a whole, it seems to skip the part where it makes the window transparent. Even if I copy and paste a block of code that includes the 'attributes("-alpha", 0.1)' into the python shell, it ignores that line.</p>
  
  <pre><code>from Tkinter import *
  
  root = Tk()
  root.attributes('-alpha', 0.1)
  
  maxW = root.winfo_screenwidth()
  maxH = root.winfo_screenheight()
  
  root.geometry("{0}x{1}+0+0".format(maxW, maxH))
  
  def callback(event):
      print "clicked at: ", event.x, "and: ", event.y
  
  root.bind("&lt;Button-1&gt;", callback)
  
  def Exit(event):
      root.destroy()
  root.bind("&lt;Escape&gt;", Exit)
  
  
  # root.overrideredirect(True)
  
  root.mainloop()
  </code></pre>
  
  <p>I am open to using any c or c++ code, or any language's code, to return the coordinates of the mouse on a click. <a href="http://hints.macworld.com/article.php?story=2008051406323031" rel="nofollow">This guy</a> wrote some code to actually make the computer click at given points, which may be on the same track as my problem.</p>