2022-01-27-opencv-and-gtk.md (1072B)
1 title: Finding mouse location in Python + GTK + OpenCV. 2 date: 2022-01-27 3 4 Expanding on the tutorial: 5 6 <https://nofurtherquestions.wordpress.com/2017/03/03/python-3-5-gtk-3-glade-and-opencv/> 7 8 it originally had shown the following structure: 9 10 ``` 11 - window1 (GtkWindow) 12 - box1 (GtkBox) 13 - grayscaleButton (GtkToggleButton) 14 - image (GtkImage) 15 ``` 16 17 According to <https://stackoverflow.com/a/24388310/189995> `GtkImage` does not 18 generate events, so if you want to find the mouse location, put your 19 `GtkImage` inside a `GtkEventBox`: 20 21 ``` 22 - window1 (GtkWindow) 23 - box1 (GtkBox) 24 - grayscaleButton (GtkToggleButton) 25 - eventbox (GtkEventBox) 26 - image (GtkImage) 27 ``` 28 29 and in `eventbox` set the `Common -> Events -> Pointer Motion` so you can add 30 a handler to the `motion-notify-event` event. 31 32 Now you can write your handler: 33 34 ```python 35 class Handler: 36 … 37 … 38 … 39 def onMotionNotifyEvent(*args): 40 global current_mouse_location 41 42 event = args[1] 43 44 current_mouse_location = event.x, event.y 45 ```