This is a pygtk treeview setup for attaching models to several similar views that will have the same look and feel such as font family, color, size, etc. You could have one for text, one for toggles, another for pixmaps. Nothing monumental here, just another method of my madness.
def tv_text_setup(self, widget, strCols):
'''setup treeview columns'''
for index in range(len(strCols)):
# Create a renderer
cell = gtk.CellRendererText()
# Set the font using pango
font = pango.FontDescription('system bold 8')
cell.set_property('font-desc', font)
# Set some more properties
cell.set_property('cell-background', 'green')
cell.set_property('width', 5)
# Create a column
tvcol = gtk.TreeViewColumn(strCols[index], cell, text=index)
# Add column to the widget
widget.append_column(tvcol)
# Column display
tvcol.pack_start((cell), True)
tvcol.set_sort_column_id(index)
tvcol.set_attributes(cell, text=index)
Now in your constructor, you can attach the models like this: (i’m using gtkbuilder to create some of the basic stuff but I make my own data stores)
class MyClass:
def __init__(self):
self.builder = gtk.builder()
self.builder.add_from_file("myclass.glade")
'''Repeat this section for each data store you want a treeview for'''
self.treeview1 = self.builder.get_object("treeview1")
self.liststore1 = gtk.ListStore(str, str, str)
self.treeview1.set_model(self.liststore1)
strCols = ['Description', 'Model', 'Device']
self.tv_text_setup(self.treeview1, strCols)