views:

374

answers:

3

How can I word wrap text inside a PyGTK TreeView?

A: 

Hi,

It seems this isn't a built-in feature of GTK, however you can create your own TreeCellRenderer, as detailed below:

http://danielwould.wordpress.com/2010/01/02/maemo-custom-cell-renderer-for-gtk-treeview-python/

http://www.islascruz.org/html/index.php?blog/show/Wrap-text-in-a-TreeView-column.html

seems pretty complicated though.

Steven Sproat
+2  A: 

Text in a gtk.TreeView is rendered using a gtk.CellRendererText, and wrapping text comes down to setting the right properties on your cell renderer. In order to get text to wrap, you need to set the wrap-width property (in pixels) on the cell renderer. You probably also want to set the wrap-mode property to something sensible. For example:

renderer.props.wrap_width = 100
renderer.props.wrap_mode = gtk.WRAP_WORD

Unfortunately, if you want adjustable-width word wrapping on a column, PyGTK won't do that for you automatically. You should be able to dynamically set wrap-width to get the right effect though; there are known workarounds like this for gtk.Label, and the guides linked in sproaty's answer seem to do a similar thing.

Kai
A: 

The answer directing at my blog post, was from before I figured out how to do it 'properly' as Kai already answered just setting wrap width and wrap mode works on a TextCellRenderer in my current custom renderer I use:

layout = cairo_context.create_layout()

font = pango.FontDescription("Sans")

font.set_size(pango.SCALE * (self.get_property('font_size')))

font.set_style(pango.STYLE_NORMAL)

font.set_weight(pango.WEIGHT_BOLD)

layout.set_font_description(font)

w=800 #the width I want to wrap at

layout.set_width(pango.SCALE * w)

layout.set_wrap(pango.WRAP_WORD)

layout.set_markup("my text to write out and wrap at the right width")

this obviously uses pango cairo, and you have to remember to multiple the width you want by the pango.SCALE else it is too small to see.