How to make the last gtk.TreeViewColumn resizeable?
Asked Answered
P

1

6

One can use gtk.TreeViewColumn.set_resizable(True) to make column manually resizeable... except the last column -- it always occupies the available space.

While it's sensible in most cases, I use TreeView-in-a-ScrolledView, and I'd like to be able to shrink/expand the last column too.

Currently I use the following kludge:

def add_dummy_column(treeview):

    def put_dummy_last(treeview, dummy):
        columns = treeview.get_columns()
        last = next(reversed(columns), None)
        if not last or last == dummy:
            return
        if dummy in columns:
            treeview.move_column_after(dummy, last)
        else:
            treeview.append_column(dummy)

    dummy = gtk.TreeViewColumn()
    dummy.set_min_width(1)
    dummy.set_fixed_width(1)
    dummy.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    dummy.set_resizable(False)
    dummy.set_expand(False)
    treeview.connect('columns-changed', put_dummy_last, dummy)
    return dummy

However, this dummy column tends to always get in the way: complicates the TreeView column loops etc. Is there better/more elegant solution?

Update: it actually works

While trying to forge a minimal example, I discovered that it works fine without my kludge. Here is minimal example without gtk.ScrolledWindow:

import gtk

window = gtk.Window()
window.connect('destroy', lambda *args: gtk.main_quit())

vbox = gtk.VBox()
window.add(vbox)

table = gtk.TreeView(gtk.ListStore(str, str, str))

for i in range(3):
    column = gtk.TreeViewColumn('Column {}'.format(1 + i))
    column.set_resizable(True)
    column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    column.set_min_width(20)
    column.set_fixed_width(80)
    column.set_expand(False)
    table.append_column(column)

vbox.pack_start(table)

vbox.pack_start(gtk.Statusbar(), False)

window.set_default_size(300, 300)
window.show_all()
gtk.main()

Try to shrink the last column -- it's impossible. However, if you change

vbox.pack_start(table)

to

container = gtk.ScrolledWindow()
vbox.pack_start(container)
container.add(table)

you can expand and shrink the last column with no problems.

Purree answered 23/7, 2011 at 8:45 Comment(0)
H
2

It would be easier to help if you would post a self-contained working example that we could play with! :)

However - if I understand your question correctly (which I'm not 100% sure, to be honest) - it might be that you are thinking to the problem in the wrong terms: what you call "resizing the last column" is actually "resizing the tree-view container".

If that is the case, you should simply add a resizeable container between the scrolled window and the treeview.

HTH!

Hiphuggers answered 23/7, 2011 at 9:10 Comment(3)
Your wise advice to create minimal example helped -- see above ;-)Purree
Awesome! You should however move your "answer" into an answer to your question, and then select your answer as "accepted". This way SO will show your question as "solved", which is useful for future visitors. This is also the first step to get the "self-learner badge"! :)Hiphuggers
I've been working with GTk3, with the TreeView inside a ScrolledWindow I still am unable to expand columns as much as I want (or at all, since my columns allways overflow the container). Could you answer be expanded with a working example that allows the user to expand and shrink columns to and desired size and will display all cell contense by defualtEckstein

© 2022 - 2024 — McMap. All rights reserved.