python - PyGTK TreeView : Prevent last column from growing

标签 python pygtk gtktreeview

我正在尝试制作一个具有四列的 gtk.TreeView,其中前两列可以调整大小,而后两列则不能。换句话说,我希望第三列和第四列占据固定的空间量,并且希望第一列和第二列以用户想要的任何比例共享剩余空间。

我的问题是我无法阻止最后一列的增大和缩小。下面的代码应该突出显示这个问题:如果您运行此代码并尝试调整第一列或第二列的大小,最后一列将弥补差异。

我知道我想要的东西可以实现,因为我见过其他 GTK GUI 可以做到这一点。两个例子是 Glade 中的“信号”对话框和 quodlibet 中的“歌曲列表”。感谢您提供有关如何实现此功能的任何想法。

import pygtk; pygtk.require('2.0')
import gtk
import pango

window = gtk.Window()
model = gtk.TreeStore(object)
view = gtk.TreeView(model)

columns = [
        ('first', True),
        ('second', True),
        ('third', False),
        ('fourth', False),
]
rows = [
        {
            'first': 'Lorem ipsum dolor sit amet',
            'second': 'consectetur adipiscing elit',
            'third': 'Nam justo sem',
            'fourth': 'malesuada ut ultricies ac',
        }, {
            'first': 'Praesent sit amet nibh turpis',
            'second': 'vitae lacinia metus',
            'third': 'Ut nisi lacus',
            'fourth': 'pretium a diam',
        }
]
for row in rows:
    model.append(None, (row,))

def cell_data_func(column, renderer, model, iter, field):
    text = model.get_value(iter, 0)[field]
    renderer.set_property('text', text)

for field, expand in columns:
    text_renderer = gtk.CellRendererText()
    column = gtk.TreeViewColumn(field, text_renderer)
    column.set_cell_data_func(text_renderer, cell_data_func, field)

    if expand:
        column.set_expand(True)
        column.set_resizable(True)
        text_renderer.set_property('ellipsize', pango.ELLIPSIZE_END)
    else:
        column.set_expand(False)
        column.set_resizable(False)

    view.append_column(column)

window.set_default_size(700, -1)
window.add(view)
window.show_all()
window.connect('destroy', lambda *args: gtk.main_quit())

gtk.main()

最佳答案

添加第五列,无需任何文本,可以扩展功能并享受:)

.
.
.
columns = [
        ('first', True),
        ('second', True),
        ('third', False),
        ('fourth', False),
        ('', True),
]
.
.
.

关于python - PyGTK TreeView : Prevent last column from growing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27474814/

相关文章:

python - 从使用 libglade 到 GtkBuilder 的转换步骤是什么? (Python)

treeview - Gtk.TreeView 无法添加自定义渲染器,断言 'gtk_cell_area_has_renderer (area, renderer)' 失败

c - 同时标记匹配和转义匹配外的特殊字符

python - 理解卡方特征选择的问题

Pythons Pandas,在 GroupBy 对象上应用函数 "indexes"

python - 如何在 gtk.TextBuffer 中等待用户输入暂停?

python - 让复选框在 GtkTreeView 中切换

python - 更新python文件时,Sphinx不会更新html页面

python - 从元组列表中删除部分重复的元组

python-3.x - 格莱德 & Pygtk : how to split dynamically windows?