0

Here is the simplest example I can make of what I am trying to do. It is a grid inside a panel:

#!/usr/bin/env python

import wx
import wx.grid

app = wx.App(False)  

class InfoPane(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent)

        # Set up the presentation
        self.SetRowLabelSize(0)
        self.CreateGrid(1, 6)

        self.SetColLabelAlignment( wx.ALIGN_LEFT, wx.ALIGN_CENTRE )
        self.SetColLabelValue(0, "Name")
        self.SetColLabelValue(1, "Status")
        self.SetColLabelValue(2, "")
        self.SetColLabelValue(3, "File")
        self.SetColLabelValue(4, "Last Action")
        self.SetColLabelValue(5, "Other Info")

frame = wx.Frame(None)
panel = wx.Panel(frame)
info_pane = InfoPane(panel)

note_sizer = wx.BoxSizer()
note_sizer.Add(info_pane, 1, wx.EXPAND)

panel.SetSizerAndFit(note_sizer)
frame.Show()

app.MainLoop()

I want the whole thing to size itself so that the contents of the grid are all visible. What happens at the moment is this:

screenie

(note: edit - I took the notebook out, for even more simplification, the result is the same looking)

Ideas? Thanks!

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • why not enlarge your window when you create it? frame = wx.Frame(None,size=(1000,1000)) – Jerry_Y Feb 11 '14 at 12:47
  • Because I want the app to start up the correct size. Too big: lots of yucky whitespace around the grid (try it with the example code, I think you will see what I mean). Too small: scroll bar. – GreenAsJade Feb 11 '14 at 12:49

2 Answers2

2

If you add a sizer to the frame when you call the fit method on the frame it will size to its children.

#!/usr/bin/env python

import wx
import wx.grid

app = wx.App(False)


class InfoPane(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent)

        # Set up the presentation
        self.SetRowLabelSize(0)
        self.CreateGrid(1, 6)

        self.SetColLabelAlignment(wx.ALIGN_LEFT, wx.ALIGN_CENTRE)
        self.SetColLabelValue(0, "Name")
        self.SetColLabelValue(1, "Status")
        self.SetColLabelValue(2, "")
        self.SetColLabelValue(3, "File")
        self.SetColLabelValue(4, "Last Action")
        self.SetColLabelValue(5, "Other Info")

frame = wx.Frame(None)
panel = wx.Panel(frame)
info_pane = InfoPane(panel)

note_sizer = wx.BoxSizer()
note_sizer.Add(info_pane, 1, wx.EXPAND)

panel.SetSizer(note_sizer)
frame_sizer = wx.BoxSizer(wx.VERTICAL)
frame_sizer.Add(panel, 1, wx.EXPAND)
frame.SetSizerAndFit(frame_sizer)
frame.Show()

app.MainLoop()
Yoriz
  • 3,595
  • 2
  • 13
  • 20
  • Sweet! That's what I was looking for. Is the essence of this answer that I was setting the size of the panel, but not the frame, and it is the frame that needed resizing? – GreenAsJade Feb 11 '14 at 13:26
1

You can get the size of the Grid and resize its parent panel with it.

Try this:

panel.SetSizerAndFit(note_sizer)
gridSize = info_pane.GetVirtualSize()
frame.Show() 
panel.SetSize(gridSize)
frame.Fit()
Jerry_Y
  • 1,724
  • 12
  • 9