tkinter - Get global mouse position and use in python program? -
as part of larger project, trying create snapshot tool works similar mac os x snapshot. should take first click, second click, , return image of area created square.
i have python functions take first point (x, y) , second point (x, y) , create snapshot of square points create on screenshot. missing piece getting mouse locations of initial click , second click, passing data python program create snapshot.
in other words, flow of program should be:
- first click (save x, y)
- second click (save x2, y2)
- run snapshot.py using saved clicked data return screenshot
i've found solutions can return position of pointer within frame. if helps, i'm using "import gtk" , "from xlib import display"
edit: have tried use tkinter make invisible frame covers whole screen. idea use invisible frame exact coordinates of 2 mouse clicks, , invisible frame disappear, pass coordinates on screenshot function, , done. however, code i've been writing doesn't keep frame transparent.
edit 2: code can create window, make transparent, size screen, return mouse coordinates on window. can use return mouse coordinates on 2 clicks, remove window , send coordinates snapshot code. when run below code line-by-line in python shell, works perfectly. however, whenever run code whole, seems skip part makes window transparent. if copy , paste block of code includes 'attributes("-alpha", 0.1)' python shell, ignores 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 open using c or c++ code, or language's code, return coordinates of mouse on click. this guy wrote code make computer click @ given points, may on same track problem.
it's indentation problem - bound callback in callback mistake - try 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 theory - maybe when run command line, takes longer root window appear, reason, set alpha before exists, , alpha option gets ignored. give try:
root.wait_visibility(root) root.attributes('-alpha', 0.1)
Comments
Post a Comment