Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the last gtk.TreeViewColumn resizeable?

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.

like image 206
Andrey Paramonov Avatar asked Nov 04 '22 17:11

Andrey Paramonov


1 Answers

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!

like image 91
mac Avatar answered Nov 14 '22 23:11

mac