Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mouse events outside of Tkinter window in Python

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:

  • Can Tkinter give me mouse events when the events are outside the Tkinter window.
  • If not, are there other options "out of the box" that can give me mouse X,Y and mouse events.
  • If there are no out of the box solutions, some recommendation on approach and API. (looks like win32api for windows, Quartz and/or Cocoa in Mac and Xlib in Linux)

Thanks for your time

like image 320
TravisT Avatar asked Sep 25 '15 02:09

TravisT


People also ask

How do I handle the window close event in tkinter?

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.

What is tkinter canvas?

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.

Which attribute of button class in tkinter is used to handle an event?

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.

What is tkinter event handler?

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.


1 Answers

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

like image 127
Tristan Avatar answered Oct 06 '22 00:10

Tristan