[Tutor] Testing for mouse clicks

Alan Gauld alan.gauld at yahoo.co.uk
Sun Nov 8 04:01:00 EST 2020


On 08/11/2020 02:02, Phil wrote:

>> Which GUI toolkit are you using?
> In this instance I'm using WxPython.

Ah, OK in that case you have to bind the mouse click to the widget after
you create it. The good news pretty much any widget can be bound.

cell1.Bind(wz.EVT_LEFT_UP, clickEventHandler))

> 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.

You don;t need a custom widget, wxPython lets you bind mouse events
to pretty much any kind of widget.

> Just empty cells on a panel which is why I've been working on mouse 
> coordinates. 

Sorry, there isn't a cell widget in wxPython. Exactly what kind of
widget are you using?

Show us the loop where you create the grid and we can probably
show you the bind call you need.

However, wxPython does have a wx.grid.Grid widget which sounds like
it would do exactly what you need without much extra work
on your part. Have you looked into that?

> It's just one window, or panel, divided into small squares which are 
> filled with data from the board array. 

OK, windows, panels, squares are all separate widgets. You need to be
precise. Specifically what kind of widget is the container? Which is the
individual cell? This is very important, you cannot be woolly about it.

> 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.

Again show us code! Code is much more precise than vague descriptions.
Programming is all about the detail.

> 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.

Yes, thats what I used for the oxo game in my book. Buttons are easy.
And they work with the mouse without any special binding.

But I'd check the Wx grid control too since it looks like it does
virtually everything you describe for free!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list