Resizing wxFrame to match wxGrid

I’m sure I’m missing something obvious, but what’s the most
straightforward way to resize my wxFrame so that it fits the

wxGrid exactly in the example below? If I try to query the size of
the grid via grid.GetSize(), all I get is (0,0), which isn’t

right… my final script will have variable dimensions for the grid, so I
need to be able to set the window size after the grid

is created.*

–PLB

`from wxPython.wx import *

from wxPython.grid import *

class TestGrid(wxGrid):

def __init__(self, parent, log):

    wxGrid.__init__(self, parent,

-1)

    wx.wxToolTip_Enable(True)

    wx.wxToolTip_SetDelay(1)

    self.CreateGrid(10,10)

self.EnableCellEditControl(False)

    self.EnableEditing(False)

self.EnableGridLines(True)

    self.SetDefaultColSize(5)

    self.SetDefaultRowSize(5)

    self.SetColLabelSize(15)

    self.SetRowLabelSize(15)

self.SetGridLineColour(‘BLACK’)

    rows =

self.GetNumberRows()

    cols =

self.GetNumberCols()

    for col in range(cols):

self.SetColLabelValue(col, ‘’)

    for row in range(rows):

self.SetRowLabelValue(row, ‘’)

    for row in range(rows):

        for

col in range(cols):

self.SetCellBackgroundColour(row, col, wxColour(0xDD, 0xDD,
0xDD))

    EVT_GRID_CELL_LEFT_CLICK(self,

self.OnCellLeftClick)

EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick)

def OnCellLeftClick(self, evt):

    print 'OnCellLeftClick:

(%d,%d) %s’ % (evt.GetRow(), evt.GetCol(), evt.GetPosition())

    row = evt.GetRow()

    col = evt.GetCol()

    tt = '(L) Row %d, Column %d' %

(row, col)

wx.wxToolTip_Enable(False)

self.GetGridWindow().SetToolTipString(tt)

    wx.wxToolTip_Enable(True)

    evt.Skip()

def OnCellRightClick(self, evt):

    print 'OnCellRightClick:

(%d,%d) %s’ % (evt.GetRow(), evt.GetCol(), evt.GetPosition())

    row = evt.GetRow()

    col = evt.GetCol()

    tt = '(R) Row %d, Column %d' %

(row, col)

wx.wxToolTip_Enable(False)

self.GetGridWindow().SetToolTipString(tt)

    wx.wxToolTip_Enable(True)

    evt.Skip()

class TestFrame(wxFrame):

def __init__(self, parent, log):

    wxFrame.__init__(self, parent,

-1, “wxGrid Test”, size=(640,480))

    grid = TestGrid(self,

log)

if name == ‘main’:

import sys

app = wxPySimpleApp()

frame = TestFrame(None, sys.stdout)

frame.Show(True)

app.MainLoop()

`

Peter L. Buschman wrote:

I'm sure I'm missing something obvious, but what's the most straightforward way to resize my wxFrame so that it fits the
wxGrid exactly in the example below? If I try to query the size of the grid via grid.GetSize(), all I get is (0,0), which isn't
right... my final script will have variable dimensions for the grid, so I need to be able to set the window size after the grid
is created.*

Try using grid.GetBestSize(). It does take into account the rows and cols, but I think there was a bug report about it returning a too large value (screen size) when there are lots of rows/cols so it may become more limited in the future.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!