python PyGTK TreeView中的自动换行

r8xiu3jd  于 2023-05-27  发布在  Python
关注(0)|答案(5)|浏览(307)

如何在PyGTK TreeView中对文本进行自动换行?

t8e9dugd

t8e9dugd1#

使用gtk.CellRendererText呈现gtk.TreeView中的文本,并且 Package 文本归结为在单元格呈现器上设置正确的属性。为了获得要换行的文本,需要在单元格渲染器上设置wrap-width属性(以像素为单位)。您可能还希望将wrap-mode属性设置为合理的值。例如:

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

不幸的是,如果您希望在列上实现宽度可调的自动换行,PyGTK不会自动为您实现这一点。您应该能够动态设置wrap-width以获得正确的效果; gtk. label有这样的known workarounds,sproaty的答案中链接的指南似乎也做了类似的事情。

6yoyoihd

6yoyoihd3#

针对我的博客文章的答案,是在我弄清楚如何“正确地”做之前,因为Kai已经回答了在我当前使用的自定义渲染器中设置 Package 宽度和 Package 模式在TextCellRenderer上工作:

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")

这显然使用了pango cairo,你必须记住用pango乘以你想要的宽度。SCALE否则它太小了看不见。

xjreopfe

xjreopfe4#

在寻找这个问题的解决方案时,我碰巧通过不同的来源把这个放在一起。更改列宽时,文本将动态换行:

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)
mitkmikd

mitkmikd5#

我想在Gtk 3.0的Python 3中实现这样的功能。下面的工作,使用穆拉特的答案和Gtk3文档如下所示:

# 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

相关问题