5 import  wx
.grid 
as  gridlib
 
   7 #--------------------------------------------------------------------------- 
   9 class MyCustomRenderer(gridlib
.PyGridCellRenderer
): 
  11         gridlib
.PyGridCellRenderer
.__init
__(self
) 
  13     def Draw(self
, grid
, attr
, dc
, rect
, row
, col
, isSelected
): 
  14         dc
.SetBackgroundMode(wx
.SOLID
) 
  15         dc
.SetBrush(wx
.Brush(wx
.BLACK
, wx
.SOLID
)) 
  16         dc
.SetPen(wx
.TRANSPARENT_PEN
) 
  17         dc
.DrawRectangleRect(rect
) 
  19         dc
.SetBackgroundMode(wx
.TRANSPARENT
) 
  20         dc
.SetFont(attr
.GetFont()) 
  22         text 
= grid
.GetCellValue(row
, col
) 
  23         colors 
= ["RED", "WHITE", "SKY BLUE"] 
  28             dc
.SetTextForeground(random
.choice(colors
)) 
  30             w
, h 
= dc
.GetTextExtent(ch
) 
  32             if x 
> rect
.right 
- 5: 
  36     def GetBestSize(self
, grid
, attr
, dc
, row
, col
): 
  37         text 
= grid
.GetCellValue(row
, col
) 
  38         dc
.SetFont(attr
.GetFont()) 
  39         w
, h 
= dc
.GetTextExtent(text
) 
  44         return MyCustomRenderer() 
  47 #--------------------------------------------------------------------------- 
  50     ('GridCellStringRenderer\n(the default)', 'this is a text value', gridlib
.GridCellStringRenderer
, ()), 
  51     ('GridCellNumberRenderer', '12345', gridlib
.GridCellNumberRenderer
, ()), 
  52     ('GridCellFloatRenderer', '1234.5678', gridlib
.GridCellFloatRenderer
, (6,2)), 
  53     ('GridCellBoolRenderer', '1', gridlib
.GridCellBoolRenderer
, ()), 
  54     ('MyCustomRenderer', 'This is my renderer', MyCustomRenderer
, ()), 
  58     ('GridCellTextEditor\n(the default)', 'Here is some more text', gridlib
.GridCellTextEditor
, ()), 
  59     ('GridCellNumberEditor\nwith min,max', '101', gridlib
.GridCellNumberEditor
, (5, 10005)), 
  60     ('GridCellNumberEditor\nwithout bounds', '101', gridlib
.GridCellNumberEditor
, ()), 
  61     ('GridCellFloatEditor', '1234.5678', gridlib
.GridCellFloatEditor
, ()), 
  62     ('GridCellBoolEditor', '1', gridlib
.GridCellBoolEditor
, ()), 
  63     ('GridCellChoiceEditor', 'one', gridlib
.GridCellChoiceEditor
, (['one', 'two', 'three', 'four', 
  64                                                          'kick', 'Microsoft', 'out the', 
  69     ('GridCellNumberRenderer\nGridCellNumberEditor', '20792', gridlib
.GridCellNumberRenderer
, gridlib
.GridCellNumberEditor
), 
  70     ('GridCellBoolRenderer\nGridCellBoolEditor', '1', gridlib
.GridCellBoolRenderer
, gridlib
.GridCellBoolEditor
), 
  74 class EditorsAndRenderersGrid(gridlib
.Grid
): 
  75     def __init__(self
, parent
, log
): 
  76         gridlib
.Grid
.__init
__(self
, parent
, -1) 
  79         self
.CreateGrid(25, 8) 
  84         self
.SetCellValue(0, renCol
, '''\ 
  85 Cell Renderers are used to draw 
  86 the contents of the cell when they 
  87 need to be refreshed.  Different 
  88 types of Renderers can be plugged in 
  89 to different cells in the grid, it can 
  90 even be automatically determined based 
  91 on the type of data in the cell. 
  94         self
.SetCellValue(0, edCol
, '''\ 
  95 Cell Editors are used when the 
  96 value of the cell is edited by 
  97 the user.  An editor class is 
  98 wrapped around a an object 
  99 derived from wxControl and it 
 100 implements some methods required 
 101 to integrate with the grid. 
 104         self
.SetCellValue(16, renCol
, '''\ 
 105 Here are some combinations of Editors and 
 106 Renderers used together. 
 111         for label
, value
, renderClass
, args 
in rendererDemoData
: 
 112             renderer 
= renderClass(*args
) 
 113             self
.SetCellValue(row
, renCol
, label
) 
 114             self
.SetCellValue(row
, renCol
+1, value
) 
 115             self
.SetCellRenderer(row
, renCol
+1, renderer
) 
 121         for label
, value
, editorClass
, args 
in editorDemoData
: 
 122             editor 
= editorClass(*args
) 
 123             self
.SetCellValue(row
, edCol
, label
) 
 124             self
.SetCellValue(row
, edCol
+1, value
) 
 125             self
.SetCellEditor(row
, edCol
+1, editor
) 
 131         for label
, value
, renClass
, edClass 
in comboDemoData
: 
 132             self
.SetCellValue(row
, renCol
, label
) 
 133             self
.SetCellValue(row
, renCol
+1, value
) 
 135             renderer 
= renClass() 
 136             self
.SetCellEditor(row
, renCol
+1, editor
) 
 137             self
.SetCellRenderer(row
, renCol
+1, renderer
) 
 140         font 
= self
.GetFont() 
 141         font
.SetWeight(wx
.BOLD
) 
 142         attr 
= gridlib
.GridCellAttr() 
 144         attr
.SetBackgroundColour(wx
.LIGHT_GREY
) 
 145         attr
.SetReadOnly(True) 
 146         attr
.SetAlignment(wx
.RIGHT
, -1) 
 147         self
.SetColAttr(renCol
, attr
) 
 149         self
.SetColAttr(edCol
, attr
) 
 151         # There is a bug in wxGTK for this method... 
 152         self
.AutoSizeColumns(True) 
 153         self
.AutoSizeRows(True) 
 155         self
.Bind(gridlib
.EVT_GRID_CELL_LEFT_DCLICK
, self
.OnLeftDClick
) 
 158     # I do this because I don't like the default behaviour of not starting the 
 159     # cell editor on double clicks, but only a second click. 
 160     def OnLeftDClick(self
, evt
): 
 161         if self
.CanEnableCellControl(): 
 162             self
.EnableCellEditControl() 
 165 #--------------------------------------------------------------------------- 
 167 class TestFrame(wx
.Frame
): 
 168     def __init__(self
, parent
, log
): 
 169         wx
.Frame
.__init
__(self
, parent
, -1, "Editors and Renderers Demo", size
=(640,480)) 
 170         grid 
= EditorsAndRenderersGrid(self
, log
) 
 174 #--------------------------------------------------------------------------- 
 176 if __name__ 
== '__main__': 
 178     app 
= wx
.PySimpleApp() 
 179     frame 
= TestFrame(None, sys
.stdout
) 
 184 #---------------------------------------------------------------------------