如何在PyGTK TreeView中換行文字?PyGTK TreeView中的文字換行
回答
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的答案相關的指南似乎也做了類似的事情。
你知道如何在C++版本'gtkmm-2.4'中做到這一點嗎? – Rasoul 2013-01-15 18:34:14
看來這不是一個內置的GTK的功能,但是你可以創建自己的TreeCellRenderer,詳述如下:
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
似乎很複雜,但。
答案在我的博客文章指導,從之前我想出如何做「正確」 作爲凱已經回答了剛纔設置卷繞寬度和包裝模式工作在一個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寬度否則太小看到的。
pango.SCALE原來正是我所需要的「讓」我的包裹在pangocairo完成。謝謝! – heltonbiker 2011-08-16 18:53:33
在搜索這個問題的解決方案時,我碰巧通過不同的來源將它們放在一起。當你改變列的寬度時,文本被動態地包裹起來:
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)
- 1. 的PyGTK - TreeView和選擇行
- 2. 在PyGTK中的TreeView控件
- 3. 在pygtk Treeview中添加新行
- 4. 在PyGTK TreeView中粗體/非粗體行
- 5. Treeview到Pygtk標籤
- 6. pyGTK TreeView中有條件的CellRenderCombo
- 7. PyGTK treeview和row_activated回調
- 8. 如何在PyGTK的TreeView中捕獲選定的行和列?
- 9. 抓PyGTK的TreeView的重新排序
- 10. 從pygtk的button_press_event發送treeview信息
- 11. PyGTK TreeView顯示來自ListStore的空白行
- 12. 無法讓滾動條工作PyGTK treeview
- 13. PYGTK將事件重定向到TreeView
- 14. pyGTK多行輸入字段?
- 15. 在排序的列中優化pygtk/gtk TreeView交互式搜索?
- 16. 如何獲取TreeView中元素的列表? PyGtk
- 17. 使GTK TreeView中的行不可選擇
- 18. 每次點擊切換TreeView行選擇
- 19. UIPickerView中的文字換行
- 20. JTextPane中的文字換行
- 21. 的PyGTK 2:TreeView的是伸出甚至帶滾動條啓用
- 22. 無法滾動到TreeView的結尾PyGTK/GTK
- 23. 如何在PyGTK的treeview中從選定的項目中獲取值?
- 24. pygtk中的datagridview
- 25. PyGtk中的Infobubble
- 26. 使用pygtk只更改TreeView中的節點的孩子的顏色?
- 27. PyGtk CellRendererCombo使用Pixbuf
- 28. 將PyGTK轉換爲exe
- 29. PyGTK的 - 添加行到gtk.TreeStore
- 30. pygtk中的多行gtk條目
我對PyGTK很陌生,所以如果有什麼細節我不用發表評論。 – Macha 2010-01-06 17:21:26