Neither wx.GridSizer nor wx.GridBagSizer can actually create a grid

Hi everyone

I implemented a wx GUI to build a grid to be the game’s board.

I have been trying to implement a game like this:

My problem is that wx.GridSizer or wx.GridBagSizer don’t seem to actually
create a grid. Only the last added item seems visible.

Implementation is in createGrid(), lines 28-33:

I sub classed wx.Button as to see how to trigger the game’s logic from the GUI. I would later replace it with some other colored control (window) such as those blocks shown in the game I am trying to implement.

This project has been stale like this for two months. I don’t seem to be able to figure it out and don’t know what else to do… I tested other examples, all of them worked as expediente.

Thanks for letting me be part of your community.


You can use a grid sizer with hgap/vgap, e.g. wx.GridSizer(3, 3, 2, 2) for a 3x3 grid with 2 pixels gap.
See attachment and screenshot.

I doubt that this will create a really nice result, though. You’re probably better off painting the board yourself. For games, there might be more suitable libraries, maybe pygame or something similar.

grafik (2.7 KB) (776 Bytes)

1 Like

Thanks DietmarSchwertberger your interest works as expected. The code looks like mine in my master brarnch implementation,. except that in your file, every tile is declared.

In theory, my code works, but in practice, both of my implementations look like this:


Instead of doing that, it must print a 5*7 grid of those buttons instead. Might my code actually be wrong somehow?

I’m considering using pygame also, but I have no experience with it. I’ll look into it.

I appreciate your help.


First things first, Sizers are for the layout of a collection of widgets. They don’t actually draw anything. If you need that, the best thing to do is create a EVT_PAINT event handler and draw things there.

@DietmarSchwertberger’s example works by setting the background color of the parent window, and then leaving enough space between the tile widgets that the color can be seen between them. That’s okay if it works for you, but I expect that it won’t scale well and could end up with some flicker in some situations.

Because you never called SetSizer, so the layout algorithm is never run, so the tile widgets are never moved from their default position.

The row, col indexes are zero-based. If you want to display a one-based range, then add one to the values you get.

Yes, it’s true. I didn’t notice tat was missing. Now, it actually shows as I intend for the moment:


I labeled the buttons with their positions for debugging this issue.

About that, I wasn’t going to do the aesthetic work and coding until I didn’t fix my issue. Now, I have to get the logic to work.Then, I’ll implement the aesthetics. That’s why I made the wxBlock class, so I can code in one place the look and feel of every cell according to their value.

Thanks a lot @Robin. I don’t think I would have solved it without your comment. Lesson learned|