oxidworks
oxidworks

Reputation: 1642

Autosize Python Gtk3 TreeViewColumn but make them resizable

I try to change the width of the columns. When I set them to autosize the having all a good size, but I cannot make them smaller.

Then I set the columns to fixed size but now the horizontal scrollbar no longer appears.

So maybe it is possible to start with autosize column width but also give the possibility to change the width later?

#!/usr/bin/env python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_size_request(400, 200)
mainbox = Gtk.VBox()
window.add(mainbox)

scrolled_window = Gtk.ScrolledWindow()
mainbox.pack_start(scrolled_window, True, True, 0)

transactions_liststore = Gtk.ListStore(str, str, str, str, str, str)   
for n in range(30):
    transactions_liststore.append(["A", "2016-10-10", "Ulrich Farmer", "99,99 EUR", "A short Text", "A longer Text with much much more more content"])
treeview = Gtk.TreeView(Gtk.TreeModelSort(transactions_liststore))   
scrolled_window.add(treeview)

for n, name in enumerate(["Type", "Date", "Name", "Value", "Info1", "Info2"]):
    cell = Gtk.CellRendererText()
    column = Gtk.TreeViewColumn(name, cell, text=n)

    if True:
        column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
        column.set_fixed_width(50)
        column.set_min_width(50)
        column.set_expand(True)

    column.set_resizable(True)
    column.set_reorderable(True)
    treeview.append_column(column)

window.show_all()
Gtk.main()

edit: found on https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeViewColumn.html#Gtk.TreeViewColumn.set_resizable

If resizable is True and sizing mode of the column is Gtk.TreeViewColumnSizing.AUTOSIZE, then the sizing mode is changed to Gtk.TreeViewColumnSizing.GROW_ONLY.

Upvotes: 3

Views: 1781

Answers (1)

theGtknerd
theGtknerd

Reputation: 3745

Try this code :

if True:
    column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
    #column.set_fixed_width(50)

Edit:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys


class GUI:
    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_size_request(400, 200)
        mainbox = Gtk.VBox()
        window.add(mainbox)
        window.connect('destroy', self.on_window_destroy)

        scrolled_window = Gtk.ScrolledWindow()
        mainbox.pack_start(scrolled_window, True, True, 0)

        transactions_liststore = Gtk.ListStore(str, str, str, str, str, str)    
        for n in range(30):
           transactions_liststore.append(["A", "2016-10-10", "Ulrich Farmer", "99,99 EUR", "A longer Text with much much more more content", "A short Text"])
        treeview = Gtk.TreeView(Gtk.TreeModelSort(transactions_liststore))   
        scrolled_window.add(treeview)

        for n, name in enumerate(["Type", "Date", "Name", "Value", "Info1", "Info2"]):
            cell = Gtk.CellRendererText()
            column = Gtk.TreeViewColumn(name, cell, text=n)

            column.set_min_width(50)
            column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
            column.set_resizable(True)
            column.set_reorderable(True)
            treeview.append_column(column)
        window.show_all()

    def on_window_destroy(self, window):
        Gtk.main_quit()
def main():
   app = GUI()
   Gtk.main()

if __name__ == "__main__":
   sys.exit(main())

You still cannot resize the last column but it works to resize the other columns. Hope this helps.

Upvotes: 3

Related Questions