The grid control -- problems and questions

Bob Klahn bobklahn at comcast.net
Sun Sep 2 16:53:39 PDT 2007


The attached grid code features a custom renderer.  In that renderer, 
apart from drawing the given cell, I print the row, col, and 
isSelected values to stdout, along with the number of the current 
call to the draw routine (1, 2, 3, ...).

Some questions:

     (1) How do I best avoid the excessive drawing that occurs
              (a) when switching to another application and switching back?
              (b) when resizing the test frame?

     (2) Note the rows that are redrawn as one clicks on different rows.
              (a) When one clicks on the row immediately below the current row,
                       (i) notice the color of the rightmost column
                               -- Why is that happening?
                               -- Why is it different when moving 
from row 0 to row 1, versus
                                  moving from row n to row n+1 where n <> 0 ?
                       (ii) notice which rows are redrawn, and in what order
                               -- Why so many?
              (b) When one clicks on any row not immediately below 
the current row,
                       (i) notice that the color problem seen above 
does not occur
                               -- Why not?
                       (ii) notice that fewer rows are redrawn
                               -- Why?

Note that I've created the grid with optional selmode argument 
gridlib.Grid.SelectRows.
The problems I've listed in (2) above disappear if I drop that argument.

But I'd like to use this argument.  How can I use it here without 
incurring the color problems seen in (2)(a)(i) above?

Bob
-------------- next part --------------
# TestGrid.py
# -----------

import  wx
import  wx.grid as gridlib

data = [['one'  ,'Alfa'   ]
       ,['two'  ,'Bravo'  ]
       ,['three','Charlie']
       ,['four' ,'Delta'  ]
       ,['five' ,'Echoo'  ]
       ,['six'  ,'Foxtrot']
       ,['seven','Golf'   ]
       ,['eight','Hotel'  ]
       ,['nine' ,'India'  ]
       ,['ten'  ,'Juliet' ]]

draw_ct = 0

#---------------------------------------------------------------------------

class TestRenderer(gridlib.PyGridCellRenderer):
    def __init__(self):
        gridlib.PyGridCellRenderer.__init__(self)

    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        global draw_ct

        draw_ct += 1
        print ( 'Calling Draw #%4d: row %d, col %d, sel %s'
                % (draw_ct, row, col, isSelected) )

        dc.SetBackgroundMode(wx.SOLID)

        dc.SetBrush(wx.Brush(wx.RED, wx.SOLID))
        dc.SetPen  (wx.TRANSPARENT_PEN)

        dc.DrawRectangleRect(rect)

        dc.SetBackgroundMode(wx.TRANSPARENT)
        dc.SetFont(attr.GetFont())

        text = grid.GetCellValue(row, col)
        x = rect.x + 1
        y = rect.y + 1

        for ch in text:
            dc.DrawText(ch, x, y)
            w, h = dc.GetTextExtent(ch)
            x += w

    def GetBestSize(self, grid, attr, dc, row, col):
        text = grid.GetCellValue(row, col)
        dc.SetFont(attr.GetFont())
        w, h = dc.GetTextExtent(text)
        return wx.Size(w, h)

    def Clone(self):
        return TestRenderer()

#---------------------------------------------------------------------------

class TestGrid(gridlib.Grid):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)
        self.log = log

#       self.CreateGrid( len(data), 2, gridlib.Grid.SelectRows)
        self.CreateGrid( len(data), 2                         )

        self.load_grid()

        self.EnableGridLines(False)

        self.SetRowLabelSize(0)
        self.SetColLabelSize(0)

        self.AutoSizeColumns(True)

    def load_grid(self):
        font = self.GetFont()
        font.SetWeight(wx.NORMAL)

        attr0 = gridlib.GridCellAttr()
        attr0.SetFont(font)
        attr0.SetReadOnly(True)

        attr1 = gridlib.GridCellAttr()
        attr1.SetFont(font)
        attr1.SetRenderer( TestRenderer())

        for itemx, item in enumerate(data):
            self.SetCellValue(itemx,0,item[0])
            self.SetCellValue(itemx,1,item[1])

            self.SetAttr(itemx,0, attr0)
            self.SetAttr(itemx,1, attr1)

#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, -1, "Test Grid", size=(120,220))
        grid = TestGrid(self, log)

#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys

    app = wx.PySimpleApp()
    frame = TestFrame(None, sys.stdout)
    frame.Show(True)
    app.MainLoop()

#---------------------------------------------------------------------------
-------------- next part --------------



More information about the wxpython-users mailing list