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