[Tutor] Testing for mouse clicks

Cameron Simpson cs at cskk.id.au
Sat Nov 7 21:13:03 EST 2020


On 08Nov2020 10:40, Phil <phillor9 at gmail.com> wrote:
>On 8/11/20 10:16 am, Cameron Simpson wrote:
>>Can you inspect your grid widget to determine its coordinates? Or
>>inspect the button for its coordinates? If so, measure, construct a
>>mapping of bounds to actions to take. Example:
>>
>>     widget_map = {}
>>     for my_widget in enumerate-your-widgets-here:
>>         x0, dx, y0, dy = my_widget.bounds()
>>         widget_map[x0, y0, x0+dx, y0+dy] = my_widget
>>     ...
>>     # find the widget and call its on_clicked method
>>     for (x0, x1, y0, y1), my_widget in widget_map.items():
>>         if x0 <= x < x1 and y0 < y <= y1:
>>             my_widget.on_clicked(...)
>>             break
>>
>Thank you Cameron, I would never have thought of using a for_loop with 
>multiple variables. I'll experiment with this idea and see what I can 
>come up with.

Note the brackets: you get (key,value) from .items() and we unpack the 
key into its 4 components. Look up "unpacking syntax" in the python 
docs.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list