简体   繁体   中英

Always visible and sticky columns in Gtk.TreeView

I have this Gtk.TreeView with 3 columns embeded into a 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.

#!/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 . It should be a 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 .

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.

Besides the answer provided by @RandomUser, you could also use a fixed size column. Example:

col_a.set_fixed_width(150)

Adding set_expand will provide a different action yet:

col_a.set_expand(True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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