Word Wrap in PyGTK TreeView

2019-04-11 05:35发布

How can I word wrap text inside a PyGTK TreeView?

4条回答
Evening l夕情丶
2楼-- · 2019-04-11 06:14

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.

查看更多
太酷不给撩
3楼-- · 2019-04-11 06:23

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)
查看更多
叼着烟拽天下
4楼-- · 2019-04-11 06:33

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.

查看更多
神经病院院长
5楼-- · 2019-04-11 06:38

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.

查看更多
登录 后发表回答