Basically if I drag a cell I can size it to whatever size I want. Is there functionality in wxPython where cells expand automatically to fit the size of the text inside of them?
3 Answers
One way to do it would be to catch any data entry into the cell event, get the size of the entered data, check if its greater than the column size -> if yes then set the text width as the column size.
Here's an example code I wrote:
import wx.grid
import wx
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
sizer_main = wx.BoxSizer( wx.VERTICAL )
self.panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
sizer_inner = wx.BoxSizer( wx.VERTICAL )
self.grid = wx.grid.Grid( self.panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
# Grid
self.grid.CreateGrid( 5, 5 )
self.grid.EnableEditing( True )
self.grid.EnableGridLines( True )
self.grid.SetGridLineColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_BACKGROUND ) )
self.grid.EnableDragGridSize( False )
self.grid.SetMargins( 0, 0 )
# Columns
self.grid.EnableDragColMove( False )
self.grid.EnableDragColSize( True )
self.grid.SetColLabelSize( 30 )
self.grid.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
# Rows
self.grid.EnableDragRowSize( True )
self.grid.SetRowLabelSize( 80 )
self.grid.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
# Label Appearance
# Cell Defaults
self.grid.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
sizer_inner.Add( self.grid, 1, wx.ALL, 0 )
self.panel.SetSizer( sizer_inner )
self.panel.Layout()
sizer_inner.Fit( self.panel )
sizer_main.Add( self.panel, 1, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( sizer_main )
self.Layout()
self.Centre( wx.BOTH )
self.Show()
# Connect Events
self.grid.Bind( wx.grid.EVT_GRID_CELL_CHANGE, self.on_edit )
# Some essential stuff for resizing
font = wx.Font(pointSize = 10, family = wx.DEFAULT, style = wx.NORMAL, weight = wx.NORMAL, faceName = 'Consolas')
self.dc = wx.ScreenDC()
self.dc.SetFont(font)
def on_edit( self, event ):
row = event.GetRow()
col = event.GetCol()
size = self.dc.GetTextExtent(self.grid.GetCellValue(row, col))
if size[0] > self.grid.GetColSize(col): self.grid.SetColSize(col, size[0])
self.panel.Layout()
if __name__ == "__main__":
app = wx.App()
Frame(None)
app.MainLoop()
The self.panel.Layout() is probably not needed.

- 2,267
- 1
- 14
- 25
-
Really appreciate all the functionality you demonstrated here. – Alex F Jun 26 '17 at 20:47
-
Getting nostalgic thinking about my WxPython days! If you have time to kill, check out wxFormBuilder, an amazing design tool for wxPython. – user2963623 Jul 19 '17 at 18:23
-
This looks great. Really wish I knew about this earlier! – Alex F Jul 19 '17 at 18:26
The Grid
class has AutoSizeColumn
/AutoSizeRow
and AutoSizeColumns
/AutoSizeRows
methods that do a fairly good job of generically resizing rows or cols to be large enough for the contents of the row or col. They can be fairly expensive operations for large grids however, so they should be used with care.

- 6,116
- 1
- 15
- 14
You might be able to use wxPython's wordwrap module or a custom renderer or a combination of the two. This other Stack Answer actually uses both:
Looking at the source code in the wxPython demo leads me to believe that you can probably do this with just wordwrap as the examples in GridStdEdRend.py are just using strings that are enclosed with triple quotes. You might want to take a look at that as well.

- 1
- 1

- 32,629
- 8
- 45
- 88