Macha
Macha

Reputation: 14664

Word Wrap in PyGTK TreeView

How can I word wrap text inside a PyGTK TreeView?

Upvotes: 4

Views: 3095

Answers (5)

John
John

Reputation: 222

I was wanting something like this for Python3 with Gtk 3.0. The following works, using Murat's answer and the Gtk3 docs as noted below:

# https://stackoverflow.com/questions/2014804/
# https://docs.gtk.org/gtk3/enum.WrapMode.html
renderer.props.wrap_width = 100
renderer.props.wrap_mode = Gtk.WrapMode(0) # Can be 1 or 2

Upvotes: 0

Murat G
Murat G

Reputation: 61

While searching for a solution to this question I happen to put this together via different sources. When you change the column width, the text is wrapped dynamically:

def set_column_width(column, width, renderer, pan = True):
   column_width = column.get_width()
   #print "column %s size %s" % (column.get_title(), column_width)
   renderer.props.wrap_width = column_width
   if pan:
      renderer.props.wrap_mode = pango.WRAP_WORD
   else:
      renderer.props.wrap_mode = gtk.WRAP_WORD


cell_renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn()

column.connect_after("notify::width", set_column_width, cell_renderer)

Upvotes: 0

user282107
user282107

Reputation: 11

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.

Upvotes: 1

Kai
Kai

Reputation: 5340

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.

Upvotes: 6

Steven Sproat
Steven Sproat

Reputation: 4578

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.

Upvotes: 2

Related Questions