Python GTK: Treeview with rows of different colors
Articles may may have files attached at the end of the post
With a simple example, I am going to illustrated how to build a ListStore TreeView in pygtk (python's gtk binding).
Building a standard ListStore in pygtk is pretty straightforward, however, it can get a bit more difficult to set up a custom TreeView where the rows and columns that are displayed can have different colors.
This tutorial will show how-to create and set up a TreeView, its TreeViewColumn and finally, the CellRenderer.
TreeView with different row and column colors
For the purpose of this tutorial, we are going to create a gtk.Window in which we are going to fit a gtk.TreeView. The TreeView will then be customized so we can feed the text of the 2 column (1st and 2nd parameters to liststore.append() and the color of the foreground and background of the cells (3rd and 4th parameter of liststore.append()).
The second column will only decorate the cells with the supplied colors if it is told to do so (5th parameter of liststore.append()).
Here is the sample code used to generate the screenshot above:
- #!/usr/bin/env python
- try:
- import gobject, gtk, pango
- except:
- print >> sys.stderr, _("You need to install the python bindings for " \
- "gobject, gtk and pango to run this example.")
- sys.exit(1)
- DATA = [
- {'col1' : 'row 1/col 1', 'col2' : 'row 1/col 2', 'fg': '#000000', 'bg': '#FF00FF', 'paint_2nd_row' : False},
- {'col1' : 'row 2/col 1', 'col2' : 'row 2/col 2', 'fg': '#FF0F0F', 'bg': '#C9C9C9', 'paint_2nd_row' : False},
- {'col1' : 'row 3/col 1', 'col2' : 'row 3/col 2', 'fg': '#000FFF', 'bg': '#FFF000', 'paint_2nd_row' : True},
- ]
- class Window(gtk.Window):
- def __init__(self):
- gtk.Window.__init__(self)
- self.set_title("ListStore Example")
- self.connect("destroy", lambda w: gtk.main_quit())
- self.connect("delete_event", lambda w, e: gtk.main_quit())
- #we create the store and append our elements
- liststore = gtk.ListStore(str, str, str, str, bool)
- for i in DATA:
- liststore.append([i['col1'], i['col2'], i['fg'], i['bg'],i['paint_2nd_row']])
- treeview = gtk.TreeView(liststore)
- #First column's cell
- cell = gtk.CellRendererText()
- col = gtk.TreeViewColumn("Column 1")
- col.pack_start(cell, True)
- #the first column is always painted
- cell.set_property('background-set' , True)
- cell.set_property('foreground-set' , True)
- col.set_attributes(cell,text=0, foreground=2, background=3)
- treeview.append_column(col)
- #Second column's cell
- cell = gtk.CellRendererText()
- col = gtk.TreeViewColumn("Column 2", cell, text=1, foreground=2, background=3, foreground_set=4, background_set = 4)
- treeview.append_column(col)
- self.add(treeview)
- self.show_all()
- if __name__ == "__main__":
- #window creation
- win = Window()
- #enter the main loop
- gtk.main()
Now, we are going to go over the code and details what is being done.
| Attachment | Size |
|---|---|
| pygtk-treeview-different-row-columns-colors.py.txt | 1.76 KB |
Similar entries
- Python GTK: Treeview with rows of different colors -- page 2
- TreeView with different row and column colors
- C GTK+ : Text Completion on a GtkEntry with GtkEntryCompletion
- Python GTK: How to set up gtk.Notebook tab with custom widget
- Python: Embedding a Virtual Terminal in a GTK widget with python vte library












