4

I'm making my first foray into GUI programming, and I'm trying to get to grips with wxPython. I'm trying to use wxGlade, but it's turning out to be a bit buggy.

I'm making a layout using GridSizer.

I've worked out that every time you add something to the sizer, it gets put in the next cell. This means if you have an empty cell, you need to fill it with something. Am I right?

This is the layout I'm going for (wxGlade screenshot):

wxGlade layout screenshot

The problem is, generating code from that I get this:

enter image description here

    grid_sizer_1 = wx.GridSizer(3, 3, 0, 0)
    grid_sizer_1.Add(self.button_last_page, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add(self.button_up, 0, wx.ALIGN_BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 0)
    grid_sizer_1.Add(self.button_next_page, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add(self.button_left, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add(self.button_select, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add(self.button_right, 0, wx.ALIGN_CENTER_VERTICAL, 0)
    grid_sizer_1.Add(self.button_down, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)

Seemingly because the "Down" button is getting put in the 7th cell instead of the 8th.

What is the standard way of dealing with this? Would you put some kind of dummy widget in to fill the empty cell? If so which widget? Or am I using the wrong kind of sizer?

Thanks!

2 Answers 2

9

As you said... adding a dummy widget (blank static text) works well. You can also use AddMany() instead of multiple add()'s.

grid_sizer_1 = wx.GridSizer(3, 3, 0, 0)
grid_sizer_1.AddMany( [
  (self.button_last_page, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL),
  (self.button_up, 0, wx.ALIGN_BOTTOM|wx.ALIGN_CENTER_HORIZONTAL),
  (self.button_next_page, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL),
  (self.button_left, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL),
  (self.button_select, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL),
  (self.button_right, 0, wx.ALIGN_CENTER_VERTICAL),
  (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
  (self.button_down, 0, wx.ALIGN_CENTER_HORIZONTAL) ] )
2
  • 1
    Thanks for the AddMany tip! How about (0,0) as a dummy widget? Don't think you can get much simpler than that :)
    – Acorn
    Commented Jan 30, 2011 at 0:18
  • Thanks for the nice idea. I was adding panels and between textctrl and it was counted for the tabs I pressed so to move between two textctrl I had to press multiple tabs to cover the panels between Commented Jan 16, 2015 at 11:32
5

I am more familiar with using (0,0) which means add stretchable size to the sizer. So, one would create a single object like empty_cell = (0,0) and then stick in empty_cell everywhere that empty space is needed in your sizer (where for example the wx.StaticText is used in the accepted answer).

1
  • both solutions worked for me. However with the solution with StaticText, the empty space is visible because of the difference in default color of the StaticText and the panel. With emptycell = (0,0), this problem doesnt occur. Hence, this is for me the more appropriate answer Commented Apr 19, 2019 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.