]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-14/grid_renderer.py
   5 class RandomBackgroundRenderer(wx
.grid
.PyGridCellRenderer
): 
   7         wx
.grid
.PyGridCellRenderer
.__init
__(self
) 
  10     def Draw(self
, grid
, attr
, dc
, rect
, row
, col
, isSelected
): 
  11         text 
= grid
.GetCellValue(row
, col
) 
  12         hAlign
, vAlign 
= attr
.GetAlignment() 
  13         dc
.SetFont( attr
.GetFont() ) 
  15             bg 
= grid
.GetSelectionBackground() 
  16             fg 
= grid
.GetSelectionForeground() 
  18             bg 
= random
.choice(["pink", "sky blue", "cyan", "yellow", "plum"]) 
  19             fg 
= attr
.GetTextColour() 
  21         dc
.SetTextBackground(bg
) 
  22         dc
.SetTextForeground(fg
) 
  23         dc
.SetBrush(wx
.Brush(bg
, wx
.SOLID
)) 
  24         dc
.SetPen(wx
.TRANSPARENT_PEN
) 
  25         dc
.DrawRectangleRect(rect
)            
  26         grid
.DrawTextRectangle(dc
, text
, rect
, hAlign
, vAlign
) 
  29     def GetBestSize(self
, grid
, attr
, dc
, row
, col
): 
  30         text 
= grid
.GetCellValue(row
, col
) 
  31         dc
.SetFont(attr
.GetFont()) 
  32         w
, h 
= dc
.GetTextExtent(text
) 
  36         return RandomBackgroundRenderer() 
  38 class TestFrame(wx
.Frame
): 
  40         wx
.Frame
.__init
__(self
, None, title
="Grid Renderer", 
  43         grid 
= wx
.grid
.Grid(self
) 
  44         grid
.CreateGrid(50,50) 
  46         # Set this custom renderer just for row 4 
  47         attr 
= wx
.grid
.GridCellAttr() 
  48         attr
.SetRenderer(RandomBackgroundRenderer()) 
  49         grid
.SetRowAttr(4, attr
) 
  53                 grid
.SetCellValue(row
, col
, 
  54                                   "cell (%d,%d)" % (row
, col
)) 
  56 app 
= wx
.PySimpleApp()