3

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.

oxidworks
  • 1,563
  • 1
  • 14
  • 37

1 Answers1

3

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.

theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • Thanks for you comment. That is my problem: When I set them to autosize the having all a good size, but I cannot make them smaller. – oxidworks Dec 03 '16 at 15:10
  • Are you trying to make them smaller than the column name? And which column are you trying to make smaller? – theGtknerd Dec 03 '16 at 17:55
  • Not smaller then the column name but then the content text. For example, think about Info1 has one row with very long text content, but you want the row very small because it is more necessary to see Info2 rows :-) – oxidworks Dec 03 '16 at 19:07
  • I have edited my answer with code that works for me, except the last column still is fixed. – theGtknerd Dec 04 '16 at 18:28