【问题标题】:Always visible and sticky columns in Gtk.TreeViewGtk.TreeView 中始终可见和粘性列
【发布时间】:2019-03-27 23:21:19
【问题描述】:

我有这个Gtk.TreeView,在Gtk.Paned 中嵌入了 3 列。

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

我希望最后两列始终可见粘在右侧,而不会丢失左列。

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

应该是什么样子

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

我在文档中没有看到实现这一点的方法。在该示例中,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()

【问题讨论】:

    标签: python python-3.x treeview gtk pygobject


    【解决方案1】:

    至少需要进行两项更改:

    1. 要使用长字符串使列保持正常大小,请设置GtkCellRendererTextellipsize 属性。应该是PangoEllipsizeMode

    2. 要使两个右列粘在末尾,请将expand 属性设置为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,以前从未使用过。

    【讨论】:

      【解决方案2】:

      除了@RandomUser 提供的答案,您还可以使用固定大小的列。示例:

      col_a.set_fixed_width(150)
      

      添加 set_expand 将提供不同的操作:

      col_a.set_expand(True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 2015-09-12
        • 2013-07-30
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        相关资源
        最近更新 更多