I am writing a program that connects to design software, like an external plugin, then monitors mouse movement and events. It will take actions based on to the state of the connected software mouse events.
The idea is the external application will have the capability to interface to multiple similar programs instead of program specific plugins.
The prototype, and potentially the production software, is being written in python using the Tkinter GUI library. The attempt is to make it simple and portable.
I was easily able to grab "OS" level mouse position (true screen X,Y) without the Tkinter window needing to be active. My issue is that I am not finding a way to easily get the mouse click events from Tkinter. I can only gather events when over the active Tkinter window.
Tkinter provides this for mouse position:
coord = self.gui.winfo_pointerxy()
I cannot find similar for mouse events. There are libraries like selfspy and Pymouse (now part of PyUserInput) that monitor events, but are mainly for control of the input devices. My backup idea is to use these libraries for reference on writing my own input monitoring program.
My two main platforms are Mac and Windows.
Long story short:
Thanks for your time
Tkinter provides a custom handler to close the window. It acts as a callback function that the user can run in order to close the window. To close the window using the handler, we can use the destroy() method. It closes the window abruptly after calling it in any function or any widget.
A tkinter canvas can be used to draw in a window. Use this widget to draw graphs or plots. You can even use it to create graphical editors. You can draw several widgets in the canvas: arc bitmap, images, lines, rectangles, text, pieslices, ovals, polygons, ovals, polygons, and rectangles.
The Button widget is useful for handling such events. We can use Button widget to perform a certain task or event by passing the callback in the command. While giving the command to the Button widget, we can have an optional lambda or anonymous functions which interpret to ignore any errors in the program.
In tkinter, event handling is as simple as adding a command, which we'll make into a function. Even though this function we create is a basic 1-line function that simply calls another function, we can see how we can later create more complex functions for our events.
This way you get the mouse position with TKinter.
import Tkinter as tk
root = tk.Tk()
def motion(event):
x, y = event.x, event.y
print('{}, {}'.format(x, y))
root.bind('<Motion>', motion)
root.mainloop()
There is a cross-platform module pyautogui (works for os x, windows, linux).
pyautogui.posistion() //returns tuple with mouse x and y values
// same for python 3.x
refer to this article how to detect the mouse clicks for windows
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With