[Tutor] Testing for mouse clicks

Phil phillor9 at gmail.com
Sat Nov 7 21:02:54 EST 2020


On 8/11/20 10:47 am, Alan Gauld via Tutor wrote:
> On 07/11/2020 23:41, Phil wrote:
>> Spurred on by success with my recent project involving a grid of lists,
Thank you Alan for your reply.
> Which GUI toolkit are you using?
In this instance I'm using WxPython.
>
> if 136 <= x <= 156 and 370 <= y <= 390:
>       do_something
> elif 186 <= x <= 206 and 370 <= y <= 390:
>       do_something
>
> See thats exactly what i would NOT do.
I can see why, it's a real pain if I decide to add another widget to 
click on or resize the window.
>> Its far too fragile, if any of those widgets changes size you
>> are in a mess. Better to express it in terms of the widgets
>> sizes. Especially if your grid has cells of equal size - at
>> least within a column or row. Then it becomes a matter of
>> (admittedly tedious) arithmetic rather than specifying each
>> location.
>>
>> But that's rarely needed since most widgets have ways of detecting
>> if a mouse click happened.
I use two for loops (x and Y) to create a grid. If I knew how to create 
a custom widget then I suppose I could bind a mouse click to it. Perhaps 
I should investigate that idea first.
>>   So in general when you create the grid
>> you can assign a mouse click handler to that cell. (You normally
>> only need a single handler for the whole grid, just assign
>> different x,y coordinates  in a lambda:
>>
>> def mouseClickHandler(x,y):
>>      # handle it here using x,y
>>
>> cell1 = Cell(....
>>               mouseclick = lambda x=0,y=0: mouseClickHandler(x,y)
>>               ...)
>> cell2 = Cell(....
>>               mouseclick = lambda x=0,y=1: mouseClickHandler(x,y)
>>               ...)
>> etc...
>>
>> Now when the mouse is clicked in cell1 it calls mouseClickHandler(0,0)
>> and when the mouse is clicked in cell2 it calls mouseClickHandler(0,1)
>>
>> So now you need to find out how/if your GUI toolkit binds mouseclicks to
>> cells. That will depend on the toolkit you are using and exactly what
>> kind of widget your cell consists of (Entry, Label,Canvas???).
Just empty cells on a panel which is why I've been working on mouse 
coordinates. Another GUI library, (based on Tkinter)  uses what the 
author calls a Waffle which, it seems, can be bound to mouse events. I 
think I should look into creating a similar widget, it would save a lot 
of calculator time and trouble. What I've done in years past, starting 
with a Sudoku solver, was to create a board of x and y cells and, once 
those cell have been manipulated, simply use the GUI to display the result.
>> For example in Tkinter you'd use the bind() method immediately after
>> creating the cell. Something like:
>>
>> grid[x][y] = Label(gridFrame, text=....)
>> grid[x][y].bind("<Button-1>", lambda X=x, Y=y: mouseClickHandler(X,Y)
>> grid[x][y].grid(column=x,row=y,...)
>>
>> Now, if I have a 15 * 15 grid of window locations that I want to test
>> then repeating the above 225 times becomes a bit untidy, and tedious.
>> How might I best attack this mouse_testing problem?
> If they are individual windows then your window widget almost
> certainly allows binding of mouse events. You just need to find
> out how.
It's just one window, or panel, divided into small squares which are 
filled with data from the board array. Something like panel 
coordinate[x][y] = board[x][y]. I did this with Conway's Game of Life. I 
simple painted a square green if the cell was alive.
>
> Then it becomes a matter of 225 mouse bindings which is marginally easier.
>
> But more likely you will create the grid using nested for loops
> and the cell coordinates will be the loop variables. So it will
> only be one line of code(as in the Tkinter example above).
>
> But we can't be more specific without you showing us how you
> create your cells and telling us which GUI toolkit you use.

More food for thought. I can see that I need to give this much more 
consideration. Maybe a grid of buttons? WxPython buttons are just a 
clickable patch (they don't look like the more usual button, not under 
Linux at least) but it might be a good place to start.

Thank you for spurring me on.

-- 

Regards,
Phil



More information about the Tutor mailing list