[pygtk] Formatting floats in treeview

Neil Dugan neil_dugan at kooee.com.au
Fri Jun 20 16:49:06 WST 2008


Jeremy S wrote:
> If you put a float in a treeview, you get a really long decimal, which
> is almost always not desired.  So, after playing around with it for a
> while, I came up with this code to truncate floats at 1 decimal place:
>             column.set_cell_data_func(renderer,
>                 lambda column, cell, model, iter:
> cell.set_property('text', '%.1f' % model.get_value(iter,
> column.get_sort_column_id())))
> 
> The problem with this is that it uses
> GtkTreeViewColumn::get_sort_column_id() to get the column number,
> which only works if the column is sortable.  Can anyone tell me what
> to do if I want the formatting to work on any column, not just
> sortable columns?  Thank you.

I use a text renderer with a right justify setting.  And convert the 
float to a string for the model.  That way I can have any number of 
decimal points I feel like.

right = gtk.CellRendererText()
right.set_property('xalign',1.0)
column = gtk.TreeViewColumn('number',right)
view.append_column(column)

Hope this helps..

Regards Neil.



More information about the pygtk mailing list