How to set size of wx.grid's columns expand to its parent?

1,591 views
Skip to first unread message

steve

unread,
Jul 17, 2014, 5:01:17 PM7/17/14
to wxpytho...@googlegroups.com

Hi,

I try to place a wx.grid on a panel, and make its columns expand to parent, but I get a white space on the right side of cells:


I do:

import wx.grid as gridlib


class SidePanelGrid(gridlib.Grid):
    def __init__(self, parent):
        gridlib.Grid.__init__(self, parent, -1, name="Side Panel Capacity Grid")
        self.CreateGrid(3,4)
        self.HideRowLabels()
        self.HideColLabels()
        self.ShowScrollbars(wx.SHOW_SB_NEVER,wx.SHOW_SB_NEVER)
        widt, heig =  self.GetSize()
        print "widt: ", widt
        for row in xrange(3):
            self.DisableRowResize(row)
            for col in xrange(4):
                self.SetCellAlignment(row, col, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)

        for col in xrange(4):
            self.SetColSize(col, (widt/4))
            self.DisableColResize(col)


self.my_grid = SidePanelGrid(self)

It doesn't work. What do I do wrong above?

Nathan McCorkle

unread,
Jul 17, 2014, 5:15:00 PM7/17/14
to wxpytho...@googlegroups.com
We'll need to see more code.

Nathan McCorkle

unread,
Jul 17, 2014, 5:15:59 PM7/17/14
to wxpytho...@googlegroups.com
Have you placed the grid in a sizer, and called yourPanel.SetSizer(yourSizer)?


On Thursday, July 17, 2014 2:01:17 PM UTC-7, steve wrote:

steve

unread,
Jul 17, 2014, 5:19:59 PM7/17/14
to wxpytho...@googlegroups.com
I did:

self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(self.my_grid, 0, wx.EXPAND|wx.GROW)
self.SetSizer(self.mainSizer)

Mike Driscoll

unread,
Jul 17, 2014, 5:23:59 PM7/17/14
to wxpytho...@googlegroups.com

You don't set the size of the grid, so it's just using the minimum amount of size necessary. If you want a more accurate size, you would be better off changing that line to

self.GetTopLevelParent().GetSize()

This will get you a lot closer to what you want. When I tried it, the grid was actually wider than the screen and had scrollbars. I personally prefer that to the white background. If you want, you could set the size of the frame and use that size for your grid as well.

- Mike

Mike Driscoll

unread,
Jul 17, 2014, 5:26:11 PM7/17/14
to wxpytho...@googlegroups.com


On Thursday, July 17, 2014 4:19:59 PM UTC-5, steve wrote:
I did:

self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(self.my_grid, 0, wx.EXPAND|wx.GROW)
self.SetSizer(self.mainSizer)

wx.EXPAND is the same thing as wx.GROW.

- Mike

steve

unread,
Jul 17, 2014, 5:28:31 PM7/17/14
to wxpytho...@googlegroups.com
But I set the size of each column to 1/4 of grid size (there are 4 columns), so why is there whitespace on the right side of the cells?

self.SetColSize(col, (widt/4))

Mike Driscoll

unread,
Jul 17, 2014, 5:41:11 PM7/17/14
to wxpytho...@googlegroups.com


On Thursday, July 17, 2014 4:28:31 PM UTC-5, steve wrote:
But I set the size of each column to 1/4 of grid size (there are 4 columns), so why is there whitespace on the right side of the cells?

self.SetColSize(col, (widt/4))


Because the grid is expanding, but the columns are not. The columns don't expand when you change the size of your frame. If you want that to happen, you would have to catch EVT_SIZE and modify the grid's column sizes yourself.

Mike

steve

unread,
Jul 17, 2014, 5:45:15 PM7/17/14
to wxpytho...@googlegroups.com
But the whitespace is there when I first start the application as well. I don't resize the window manually.

Mike Driscoll

unread,
Jul 17, 2014, 5:52:19 PM7/17/14
to wxpytho...@googlegroups.com


On Thursday, July 17, 2014 4:45:15 PM UTC-5, steve wrote:
But the whitespace is there when I first start the application as well. I don't resize the window manually.

You told the grid to expand in the sizer. It is expanding to fit the width of the frame. That's why you see the whitespace.

Mike

Werner

unread,
Jul 18, 2014, 2:11:06 AM7/18/14
to wxpytho...@googlegroups.com
Hi Steve,


On 7/17/2014 23:19, steve wrote:
I did:

self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(self.my_grid, 0, wx.EXPAND|wx.GROW)
wx.GROW is alias of wx.EXPAND, don't know what happens if you use both at the same time, don't they negate each other?

wx.GROW
8192
wx.EXPAND
8192

Werner

Tim Roberts

unread,
Jul 18, 2014, 11:59:50 AM7/18/14
to wxpytho...@googlegroups.com
Honestly, aren't they teaching you kids Boolean algebra in high school any more?  "|" is the logical "or" operator.  1|1 == 1.  In fact, for any x, x|x == x.
-- 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Werner

unread,
Jul 18, 2014, 12:28:54 PM7/18/14
to wxpytho...@googlegroups.com
On 7/18/2014 17:59, Tim Roberts wrote:
Werner wrote:
Hi Steve,

On 7/17/2014 23:19, steve wrote:
I did:

self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(self.my_grid, 0, wx.EXPAND|wx.GROW)
wx.GROW is alias of wx.EXPAND, don't know what happens if you use both at the same time, don't they negate each other?

wx.GROW
8192
wx.EXPAND
8192

Honestly, aren't they teaching you kids Boolean algebra in high school any more?  "|" is the logical "or" operator.  1|1 == 1.  In fact, for any x, x|x ==
Kids, not sure I qualify for that :-)  and no never had algebra but then I didn't do high school or the equivalent when I want to school back a little while ago ;-) ., ,, but I know I should one day maybe learn this stuff.

Werner

Michael Moriarity

unread,
Jul 18, 2014, 2:50:05 PM7/18/14
to wxpytho...@googlegroups.com
On Fri, Jul 18, 2014 at 11:59 AM, Tim Roberts <ti...@probo.com> wrote:
Honestly, aren't they teaching you kids Boolean algebra in high school any more?  "|" is the logical "or" operator.  1|1 == 1.  In fact, for any x, x|x == x.

Well actually , Tim, "|" is the bitwise or operator; "or" is of course the logical or operator.

--
Best Regards,
Michael Moriarity
Reply all
Reply to author
Forward
0 new messages