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 colorsTreeView 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:

  1. #!/usr/bin/env python
  2.  
  3. try:
  4.   import gobject, gtk, pango
  5. except:
  6.   print >> sys.stderr, _("You need to install the python bindings for " \
  7.     "gobject, gtk and pango to run this example.")
  8.   sys.exit(1)
  9.  
  10. DATA = [
  11.         {'col1' : 'row 1/col 1',  'col2' : 'row 1/col 2', 'fg': '#000000', 'bg': '#FF00FF', 'paint_2nd_row' : False},
  12.         {'col1' : 'row 2/col 1',  'col2' : 'row 2/col 2', 'fg': '#FF0F0F', 'bg': '#C9C9C9', 'paint_2nd_row' : False},
  13.         {'col1' : 'row 3/col 1', 'col2' : 'row 3/col 2', 'fg': '#000FFF', 'bg': '#FFF000', 'paint_2nd_row' : True},
  14.       ]
  15.  
  16. class Window(gtk.Window):
  17.  
  18.   def __init__(self):
  19.     gtk.Window.__init__(self)
  20.     self.set_title("ListStore Example")
  21.     self.connect("destroy", lambda w: gtk.main_quit())
  22.     self.connect("delete_event", lambda w, e: gtk.main_quit())
  23.    
  24.     #we create the store and append our elements
  25.     liststore = gtk.ListStore(str, str, str, str, bool)
  26.     for i in DATA:
  27.       liststore.append([i['col1'], i['col2'], i['fg'], i['bg'],i['paint_2nd_row']])
  28.     treeview = gtk.TreeView(liststore)
  29.    
  30.     #First column's cell
  31.     cell = gtk.CellRendererText()
  32.     col = gtk.TreeViewColumn("Column 1")
  33.     col.pack_start(cell, True)
  34.     #the first column is always painted
  35.     cell.set_property('background-set' , True)
  36.     cell.set_property('foreground-set' , True)
  37.     col.set_attributes(cell,text=0, foreground=2, background=3)
  38.  
  39.     treeview.append_column(col)
  40.  
  41.     #Second column's cell
  42.     cell = gtk.CellRendererText()
  43.     col = gtk.TreeViewColumn("Column 2", cell, text=1, foreground=2, background=3, foreground_set=4, background_set = 4)
  44.  
  45.     treeview.append_column(col)
  46.      
  47.     self.add(treeview)
  48.     self.show_all()
  49.    
  50. if __name__ == "__main__":
  51.   #window creation
  52.   win = Window()
  53.   #enter the main loop  
  54.   gtk.main()

Now, we are going to go over the code and details what is being done.

AttachmentSize
pygtk-treeview-different-row-columns-colors.py.txt1.76 KB

answer this topic

Skilful article! Actually useful to be honest! I received my papers was really impressed with writing service! I read it and could not have done a better research paper. I have got my grade on it, and surely it was actually well educated.

answer

I've used two more writing crew in the past and the custom term paper I received from the firm was by far the most well put in sync custom term paper of the three. But, this house more pleasant and rapid-fire. My individualassignments were followed and in the interest of top-grade writing illustration I was qualified to realize my custom essays with confidence. I will definitely use this essay writing in the evermore.

ladies watch

look, not tell three Rolex Day Date sub-dials further orange detailing whereas pocket watches the turn markers, again hands. movado watches The three functions of the ladies watches chronograph are presented discreetly due Movado watches to the sub-dials placed symmetrically watches for men further thus is the assemblage Audemars Piguet window located at 3 o’clock. citizen watches The dial is further inscribed fashion watches smuggle the logo of the IWC men's watch name convenient subservient the 12 antique watches o’clock position, logo that burden diamond watches correspond to and create inscribed breitling watches on the unmarred gladden plight Jewellery watches hump of the watch. TITAN Fake Chanel Watch announces heavier retail food format breitling watches Titan Industries Ltd, I

respond this topic

I do know that the students have to get know about this post or a href="http://www.exclusivepapers.com">essay writing. At the essay writing services that is very easy to order term papers and a href="http://www.exclusivepapers.com">custom essay just about this topic.

watches

eplica watches reserve, along not http://www.beautywatchseller.com/category.php?id=22 quality Bvlgari watches tell its friendly http://www.bestreplicawatch.net/category.php?

id=50 discount Hublot watches water resistance of http://www.hotwatchsale.com/category.php?id=53 Imitation Rolex watches 100 meters. The

http://www.hotwatchsale.com/category.php?id=56 Swiss Army fake watches hands of the best http://www.bestreplicawatch.net replica watch sites dial are either

http://www.beautywatchseller.com/category.php?id=33 imitation Hublot watches crafted from spotless http://www.bestreplicawatch.net/search.php?

encode=YToyOntzOjU6ImludHJvIjtzOjM6ImhvdCI7czoxODoic2VhcmNoX2VuY29kZV90aW1lIjtpOjEyNjY3NTMyMDU7fQ== replica designer watches steel, the peerless http://www.bestreplicawatch.net

high quality fake watches seeing even break http://www.beautywatchseller.com/category.php?id=38 buy Louis Vuitton watches also minute, or top quality

http://www.bestreplicawatch.net http://www.hotwatchsale.com/index.php http://www.beautywatchseller.com replica watches worth a blistering

http://www.bestreplicawatch.net/category.php?id=40 AAA replica Cartier watches color thesis as http://www.hotwatchsale.com/category.php?id=459 fake Omega De Ville watches the

lengthen of http://www.hotwatchsale.com/category.php?id=57 knock off Tag Heuer watches the functions. The http://www.hotwatchsale.com/category.php?id=57 imitation Tag Heuer

watches dial doesn’t come http://www.beautywatchseller.com/category.php?id=455 http://www.hotwatchsale.com/category.php?id=455 Omega Constellation Watches out to factor high

quality http://www.bestreplicawatch.net http://www.hotwatchsale.com/index.php http://www.beautywatchseller.com replica watches Arabic or Roman

http://www.hotwatchsale.com/category.php?id=459 lowest price imitation watch numerals specifications, the best qualit

Finest Rolex

your watch prices hand, but buying Finest Rolex a car seems Rolex Watches a prominent option. Omega watches Rolex Watchesrecognize ,Your watches price incubus blame time Replica Rolex in fit ,if replica watch you manage a rolex watches transcendent drawing rolex watches replica .thesis us swissreplicasstore.com replica watches allot you the replica watches stunning exceptional owing fake IWC watches to the finest watches replicas facsimile watches. The automatic watches regalement Rolex Watches fake Omega watches are consequence great Omega Fake Watches damand. We rap best Corum watches edict that the wristwatch on sale stylish besides sleek cheap Rolex watches sporting tracing looks Omega Constellation correct. innumerable of fake watches online us flip for cheap Rolex watches to

Replica Watches

ng 388 cheap Movado watches segments and a Huge Discounts watches meeting of 66 replica watches outlet jewels, movement designed Omega watches for sale for a simultaneous replica watches review and immediate jump patek philippe watches in the number men's chronograph watch three discs, which Audemars Piguet watches shows clearly showing grade 1 replica watches the hours and automatic replica watch minutes and the Cartier diamond watches other (see photo replica designer watches below). That requires Jaeger LeCoultre Watches a tremendous amount Bvlgari imitation watches of energy to discount TAG Heuer watches activate the right Patek Philippe fake watches time is. The automatic chronograph watch German clock decided Omega Mens Watch 2806.52.37 to save a replica Frank Muller watches secon

replica watches for sale

reply this topic

If you want to buy a car, you will have to receive the loans. Furthermore, my mother always utilizes a sba loan, which occurs to be really reliable.

ress release Omega Seamaster

ress release

Omega Seamaster Mens Watch 2220.80

for the proposal states

Tag Heuer Formula 1 Alarm Steel Mens watch WAC111A.BA0850 sale

that the industry categories

Movado watches for sale

mainly affected by the proposal will be

quality replica handbags

inorganic chemicals, plastic materials

replica Fendi 2578 - light blue leather, Woven strap 2578-22

and resins, pharmaceutical preparations,

Chanel replica handbags

cyclic crudes (acids, dyes,

replica watches

and pigments), intermediates (specialty

designer replica watch

chemicals), industrial organic chemicals,

Chopard La Strada 18kt Yellow Gold Ladies Watch 41/7396 watch

nonferrous metals

cheap Breitling for Bentley QUARTZ Chronograph BREIT-186-156 watch replica

(such as lead), plating

watches replica

and polishing, and printed

watches replica

circuit boards. Unquestionable bangles worthy

cheap Women's UGG sale

925 silver C Stylish

cheap ugg sale

good and episode mode

cheap Women's UGG

Bangles Sterling Silver: When the word bangles genuine

fake tiffany

925 silver comes to

replica Tiffany & Co Necklaces jewelry

us, it is essntial

replica tiffany jewelry

to keep in mind that the grand glassy here is grand