I can't seem to get the the custom grid renderers to work. Even the one included in the wxPython 2.3.1 demo doesn't seem to do anything. Specifically the Draw method is never called. Here is a condensed version of the issue that I am having (I cut the relevant sections out of the demo application)
Is this a failure of my installation or a known feature? I am running on Windows 2000 and tested with Python 2.1, Python 2.2 using both the hybrid and non hybrid versions.
Thanks for any assistance.
from wxPython.wx import *
from wxPython.grid import *
import string, random
class MyCustomRenderer(wxPyGridCellRenderer):
def __init__(self):
wxPyGridCellRenderer.__init__(self)
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
print "MyCustomRenderer.Draw called!"
dc.SetBackgroundMode(wxSOLID)
dc.SetBrush(wxBrush(wxBLACK, wxSOLID))
dc.SetPen(wxTRANSPARENT_PEN)
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
dc.SetBackgroundMode(wxTRANSPARENT)
dc.SetFont(attr.GetFont())
text = grid.GetCellValue(row, col)
colors = [wxRED, wxWHITE, wxCYAN]
x = rect.x + 1
y = rect.y + 1
for ch in text:
dc.SetTextForeground(random.choice(colors))
dc.DrawText(ch, x, y)
w, h = dc.GetTextExtent(ch)
x = x + w
if x > rect.right - 5:
break
def GetBestSize(self, grid, attr, dc, row, col):
print "getting size"
text = grid.GetCellValue(row, col)
dc.SetFont(attr.GetFont())
w, h = dc.GetTextExtent(text)
return wxSize(w, h)
def Clone(self):
return MyCustomRenderer()
class EditorsAndRenderersGrid(wxGrid):
def __init__(self, parent, log):
wxGrid.__init__(self, parent, -1)
self.log = log
self.CreateGrid(2, 2)
label = 'MyCustomRenderer'
value = 'This is my renderer'
renderer = MyCustomRenderer()
self.SetCellValue(0, 0, label)
self.SetCellValue(0, 1, value)
self.SetCellRenderer(0, 1, renderer)
self.SetCellRenderer(0, 0, renderer)
# There is a bug in wxGTK for this method...
self.AutoSizeColumns(true)
self.AutoSizeRows(true)
class TestFrame(wxFrame):
def __init__(self, parent, log):
wxFrame.__init__(self, parent, -1, "Editors and Renderers Demo", size=(640,480))
grid = EditorsAndRenderersGrid(self, log)
if __name__ == '__main__':
import sys
app = wxPySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(true)
app.MainLoop()
···
--
Brian Kelley bkelley@wi.mit.edu
Whitehead Institute for Biomedical Research 617 258-6191