2010-01-06 62 views

回答

6

gtk.TreeView中的文本使用gtk.CellRendererText呈現,並且包裝文本歸結爲在單元格渲染器上設置正確的屬性。爲了讓文本換行,您需要在單元格渲染器上設置wrap-width屬性(以像素爲單位)。您可能還想將wrap-mode屬性設置爲合理。例如:

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

不幸的是,如果你想在一列可調寬度自動換行,PyGTK中不會自動爲你做的。您應該可以動態設置wrap-width以獲得正確的效果;有像這樣的gtk.Label的known workarounds,和sproaty的答案相關的指南似乎也做了類似的事情。

+0

你知道如何在C++版本'gtkmm-2.4'中做到這一點嗎? – Rasoul 2013-01-15 18:34:14

1

答案在我的博客文章指導,從之前我想出如何做「正確」 作爲凱已經回答了剛纔設置卷繞寬度和包裝模式工作在一個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.SCALE寬度否則太小看到的。

+0

pango.SCALE原來正是我所需要的「讓」我的包裹在pangocairo完成。謝謝! – heltonbiker 2011-08-16 18:53:33

0

在搜索這個問題的解決方案時,我碰巧通過不同的來源將它們放在一起。當你改變列的寬度時,文本被動態地包裹起來:

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)