[pygtk] Table with grid-lines in it

Saftoiu, Claudiu Claudiu_Saftoiu at brown.edu
Fri Mar 28 08:31:11 WST 2008


Hello everyone,

Sorry for all the questions recently (I will get to filing earlier bug I mentioned about tooltips to bugzilla
later tonight).

I needed to display something like a table in Excel: Cells with text in them, separated by black lines.
I couldn't find an easy way to do it from my searching, so I ended up creating a LabelGridWidget. This 
is an EventBox that contains a Table. I set it's background color to be the color of the gridlines, then I 
add a ColorBin in each slot in the Table, with xoptions and yoptions to EXPAND and FILL. A ColorBin is 
another EventBox that contains a Label and changes its color. 
I then set the background color of the ColorBin  to the normal bg color (self.style.bg[gtk.STATE_NORMAL]). 
The effect is pretty much a grid, but it seems wasteful to use 26 eventboxes and 25 labels for a 5x5 table.

Is there a better way to go about doing this? I've tried using a TreeView. and have the following code
so far:

class LabelGridWidget(gtk.EventBox):
    def __init__(self, rows, cols, homogeneous=False, gridcolor=gtk.gdk.Color(0, 0, 0)):
        gtk.EventBox.__init__(self)

        self.ls = gtk.ListStore(*([gtk.Label]*cols))
        for r in xrange(rows):
            self.ls.append([gtk.Label("hello")] * cols)

        self.tv = gtk.TreeView(self.ls)
        #self.tv.set_grid_lines(True)
        self.tv.expand_all()
        self.add(self.tv)
    def settext(self, text, row, col):
        self.ls[row][col] = gtk.Label(text)
    def gettext(self, row, col):
        return self.ls[row][col].get_text()

Both with using gobject.TYPE_STRING for the columns, and with label, but I could not get it to actually display.

Thanks in advance,
- Claudiu


More information about the pygtk mailing list