python - Gtk.TreeView 中始终可见且粘性的列

标签 python python-3.x treeview gtk pygobject

我有这个 Gtk.TreeView,其中 3 列嵌入到 Gtk.Paned 中。

enter image description here

当我调整窗口大小时(或在第一列中用更长的文本填充更多行),最后两列不应消失。

我想让最后两列始终可见并且粘在右侧而不丢失左侧列。

单击按钮添加一个很长的字符串后,第一列不应增长。有点像这样(快速和肮脏的操纵屏幕截图):

它应该是什么样子

enter image description here

但它看起来如何(但不应该)

enter image description here

我在文档中看不到实现此功能的方法。在下面的示例代码中,Gtk.TreeView 被嵌入到 Gtk.ScrolledWindow 中以允许垂直滚动条。

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib

class TreeView(Gtk.TreeView):
    def __init__(self):
        # model
        self.model = Gtk.ListStore.new([str, int, int])
        for i in range(1, 6):
            self.model.append([('text {} '.format(i))*i, i*10, i])

        # view
        Gtk.TreeView.__init__(self, self.model)

        col_a = Gtk.TreeViewColumn('str',
                                   Gtk.CellRendererText(single_paragraph_mode=True),
                                   text=0)
        col_b = Gtk.TreeViewColumn('int',
                                   Gtk.CellRendererText(),
                                   text=1)
        col_c = Gtk.TreeViewColumn('int',
                                   Gtk.CellRendererText(),
                                   text=2)
        self.append_column(col_a)
        self.append_column(col_b)
        self.append_column(col_c)

        # scrollable
        self.scroll = Gtk.ScrolledWindow()
        self.scroll.add(self)
        self.scroll.set_policy(hscrollbar_policy=Gtk.PolicyType.NEVER,
                               vscrollbar_policy=Gtk.PolicyType.AUTOMATIC)

    def on_button(self, event):
        self.model.append(['long text '*20, 0, 0])

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title='Mein Gtk-Fenster')
        #self.set_default_size(100, 120)

        # tree & button
        self.view = TreeView()
        self.btn = Gtk.Button('add long row')
        self.btn.connect('clicked', self.view.on_button)

        # layout
        #box = Gtk.VBox()
        #box.pack_start(self.btn, False, False, 10)
        #box.pack_start(self.view.scroll, True, True, 10)
        #self.add(box)

        self.paned = Gtk.Paned.new(orientation=Gtk.Orientation.HORIZONTAL)
        self.paned.pack1(self.view.scroll)
        self.paned.pack2(self.btn)
        self.add(self.paned)

        self.connect('destroy', Gtk.main_quit)
        self.show_all()

if __name__ == '__main__':
    win = Window()
    Gtk.main()

最佳答案

至少需要两个改变:

  1. 要使长字符串的列保持正常大小,请设置 ellipsize GtkCellRendererText 的属性(property).它应该是 PangoEllipsizeMode .

  2. 为了让右边的两列坚持到最后,通过设置 expand 扩展第一列以占用所有剩余空间。属性(property)True .

具有这些更改的您的专栏创建:

col_a = Gtk.TreeViewColumn('str',
                           Gtk.CellRendererText(single_paragraph_mode=True,
                                                ellipsize=Pango.EllipsizeMode.END),
                           text=0)
col_a.set_expand(True)

认为您不需要 single-paragraph-mode ,以前从未使用过。

关于python - Gtk.TreeView 中始终可见且粘性的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55387959/

相关文章:

Python初学者: Setting a variable in a subclass

python - 如何将图像转换为数值数组(numpy)?

python - Python 的文件管理器

c# - Treeview 控件 - ContextSwitchDeadlock 解决方法

java - TreeView - 如何计算所有 child (包括折叠)

c++ - 为什么python 3.2的Python.h必须和Qt4一起先包含进来

python requests.text 到 pandas 数据框

python - 为什么 os.system ('cls' ) 不清除最近的输出?

python - 我怎样才能从这段代码中省略 <h> 标签?

python - 如何根据每个类别的频率对 Pandas 数据框进行子采样?