简体   繁体   English

Gtk.TreeView中始终显示可见和粘性列

[英]Always visible and sticky columns in Gtk.TreeView

I have this Gtk.TreeView with 3 columns embeded into a Gtk.Paned . 我有这个Gtk.TreeView有3列嵌入到Gtk.Paned

在此输入图像描述

When I resize the window (or fill in more rows with longer text in the first column) the last two columns shouldn't disappear. 当我调整窗口大小(或在第一列中填充更多具有更长文本的行)时,最后两列不应该消失。

I want to have the last two columns always visible and sticky to the right side without loosing the left columns. 我希望最后两列始终可见并且粘贴到右侧而不会丢失左列。

After clicking the button to add a very long string the first column should not grow. 单击按钮以添加非常长的字符串后,第一列不应增长。 Kind of this (quick & dirty manipulated screenshot): 这种(快速和肮脏的操纵截图):

How it should look like 应该怎么样

在此输入图像描述

But how it looks (but shouldn't) 但它看起来如何(但不应该)

在此输入图像描述

I don't see a way in the docu to implement this. 我没有在文档中看到实现这一点的方法。 In that example code below the Gtk.TreeView is embeded into a Gtk.ScrolledWindow to allow a vertical scrollbar. 在下面的示例代码中, 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()

There are atleast two changes needed: 至少需要进行两项更改:

  1. To keep the column a normal size with a long string, set the ellipsize property of the GtkCellRendererText . 为了保持柱正常尺寸的长字符串,请设置ellipsize的财产GtkCellRendererText It should be a PangoEllipsizeMode . 它应该是PangoEllipsizeMode

  2. To make the two right colums stick to the end, expand the first column to take all remaining space by setting the expand property to True . 要使两个右列保持结尾,请展开第一列以通过将expand属性设置为True来占用所有剩余空间。

Your column creation with these changes: 您使用这些更改创建列:

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

Don't think you need single-paragraph-mode , never used that before. 不要以为你需要single-paragraph-mode ,以前从未使用过。

Besides the answer provided by @RandomUser, you could also use a fixed size column. 除了@RandomUser提供的答案,您还可以使用固定大小的列。 Example: 例:

col_a.set_fixed_width(150)

Adding set_expand will provide a different action yet: 添加set_expand将提供不同的操作:

col_a.set_expand(True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM