cbsm1th
cbsm1th

Reputation: 230

Get global mouse position and use in python program?

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.

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.

In other words, the flow of the program should be:

  1. first click (save x, y)
  2. second click (save x2, y2)
  3. run snapshot.py using the saved clicked data to return the screenshot

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"

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.

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.

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("<Button-1>", callback)

def Exit(event):
    root.destroy()
root.bind("<Escape>", Exit)


# root.overrideredirect(True)

root.mainloop()

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. This guy wrote some code to actually make the computer click at given points, which may be on the same track as my problem.

Upvotes: 1

Views: 3188

Answers (1)

Brionius
Brionius

Reputation: 14098

It's just a indentation problem - you bound the callback in the callback by mistake - try this instead:

root.geometry("{0}x{1}+0+0".format(maxW, maxH))

def callback(event):
    print "clicked at: ", event.x, "and: ", event.y

root.bind("<Button-1>", callback)

EDIT

Ok, here's a theory - maybe when you run it from the command line, it takes longer for the root window to appear, for some reason, so you set the alpha before it exists, and the alpha option gets ignored. Give this a try:

root.wait_visibility(root)
root.attributes('-alpha', 0.1)

Upvotes: 1

Related Questions