I’m trying to write a python application that generates a green square with no filling, that follows my cursor. I want this square to always be visible, so in CSS terms, I want it’s z-index to be maximum. The way I’m thinking to implement this is to:
- First implement a recurring method that retrieves my cursor location constantly.
- Generate a green square next to it.
- Implement the method that constantly generates a green square next to my cursor, as it retrieves the location from the first method constantly.
I’ve looked into libraries such as TKInter for graphics generation, but it seems to require a canvas to be instantiated all the time. I’m also able to retrieve mouse locations currently, using Quartz.
This answer seems to do exactly what I want, but with a canvas:
Tkinter – Creating a square that follows my mouse location
Currently, I’m able to retrieve my cursor location constantly using Quartz:
import Quartz
import rumps
class MouseCoordinatesApp(rumps.App):
def __init__(self):
super(MouseCoordinatesApp, self).__init__("Mouse Coordinates")
self.timer = rumps.Timer(self.update_coordinates, 0.1)
self.timer.start()
def update_coordinates(self, _):
# Get the current mouse location
mouse_loc = Quartz.NSEvent.mouseLocation()
x, y = int(mouse_loc.x), int(mouse_loc.y)
# Update the title to show coordinates
self.title = f'X: {x}, Y: {y}'
if __name__ == "__main__":
app = MouseCoordinatesApp()
app.run()
I’ve used TKInter but it seems to require a canvas to be set first. I want the inside of the square to be completely transparent and it’s index to be prioritized.
1