Re: [pygtk] Formatting floats in treeview
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. ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
Re: [pygtk] Formatting floats in treeview
Is that code snippet of any help? def set_foreign_key(self, colnum, SQLObject, colname_1, colname_n): """maps a foreign key to a corresponding attribute in the n-table. colnum is the position of the column in the treeview (integer), SQLObject is the SQLObject class the mapped value is retrieved from, colname_1 is the column name of the foreign key field, colname_n is the column the value is retrieved from.""" column = self.get_column(colnum) cells = column.get_cell_renderers() cell = cells[0] column.set_cell_data_func(cell, self.__use_foreign_key_val, [SQLObject,colname_1,colname_n]) the only problem with this approach is that one col can have various CellRenderers; this code asserts that only one renderer is assigned to the cell. On Wednesday 18 June 2008 09:02:52 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. > ___ > pygtk mailing list pygtk@daa.com.au > http://www.daa.com.au/mailman/listinfo/pygtk > Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/ ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
[pygtk] Formatting floats in treeview
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. ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/