Gadget development for mouse coordinates
I made an automated tool recently and found that it was horrible to get the current coordinates of the mouse when developing. It’s a waste of time to use the Photoshop ruler for viewing the screenshots, so I wrote a small tool to get the coordinates. The
autopy library is used to implement the function, and
Tkinter is used to draw the interface. Convenient for further development in the next step.
When we press Enter or click the Print button, if the upper input box is non-empty, the coordinates in the frame will be printed in the bottom text box, otherwise the coordinates of the current mouse coordinates will be printed.
You can quickly record what coordinates you want to use, and then automate the processing.
autopy is a lightweight cross-platform Python library that features analog buttons and mouse, color and image processing. The image portion is not as well known as PIL, and automation is not as famous as pywin32. But its usage is simple and not limited by the platform, it is very easy to use to simulate clicks.
Tkinter is a GUI library that comes with Python. The layout is rough and the interface is simple, but its cross-platform is stable, so it is suitable for this kind of gadget. In contrast, PyQt, although rich in interface, is slightly bloated in this scenario.
- Python 2.X
First install autopy, during the installation, you may meet the following error:
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
sudo apt-get install libx11-dev libxtst-dev libpng-dev
Then install it.
sudo pip install autopy
After the installation is complete, test if you can get the coordinates.
Now writing the GUI is not complicated.
from Tkinter import *
master = Tk()
master.title("Get Point Tool")
#columnspan=2 means spanning two columns
Label(master,text = "Press Enter to get New Point").grid(row = 0, columnspan = 2)
#sticky=E means aligned to the right
Label(master, text = 'x = ').grid(row = 1, sticky = E)
Label(master, text = 'y = ').grid(row = 2, sticky = E)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row = 1, column = 1, sticky = W)
e2.grid(row = 2, column = 1, sticky = W)
#Insert coordinates into the text box
if(e1.get()=="" or e2.get()==""):
text_output.insert(0.0, "("+e1.get()+","+e2.get()+")" + "\n")
#Clear the values of the x and y text boxes. delete(0,1000) indicates the deletion of the 0th to the 1000th values.
Button(master, text = "Print", command = print_point).grid(row = 3, columnspan = 2)
#Bind the Return key to the function, note that you need to use lambada here.
text_output = Text(master)
text_output.grid(row = 4,columnspan = 2)