python-3.xlinuxgtk3glade

GTK3 + Glade doesn't auto expand ScrolledWindow to show full contents of TreeView


My example code: https://github.com/BSFEMA/GTK_Help

There are 2 programs that do the same thing here.

#1: Example_GPT.py

This was written by ChatGPT as an example to get me started. This works and the TreeView is fully displayed.

#2: Example_Glade.py + Example_Glade.glade

This was written by me using Glade for the UI. This works, but the ScrolledWindow is not expanded to fully display the contents of the TreeView widget.

Example_Glade.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name Filename -->
      <column type="gchararray"/>
      <!-- column-name Filesize -->
      <column type="gchararray"/>
      <!-- column-name Check -->
      <column type="gboolean"/>
    </columns>
  </object>
  <object class="GtkWindow" id="main_window">
    <property name="name">main_window</property>
    <property name="can-focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkTreeView" id="tree_grid">
            <property name="name">tree_grid</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="model">liststore1</property>
            <child internal-child="selection">
              <object class="GtkTreeSelection"/>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">Print Selected Files</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Example_Glade.py

import os
import sys
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk


class Main():
    def __init__(self):
        # Setup Glade Gtk
        self.builder = gtk.Builder()
        self.builder.add_from_file(os.path.join(sys.path[0], "Example_2_Glade.glade"))  # Looking where the python script is located
        self.builder.connect_signals(self)
        # Get UI components
        window = self.builder.get_object("main_window")
        window.connect("delete-event", gtk.main_quit)
        window.set_title('File Browser')
        window.show()
        #
        # Create a TreeViewColumn for the original filename
        renderer_text1 = gtk.CellRendererText()
        column_text1 = gtk.TreeViewColumn("Filename", renderer_text1, text=0)
        column_text1.set_sizing(gtk.TreeViewColumnSizing.AUTOSIZE)
        column_text1.set_resizable(True)
        # Create a TreeViewColumn for the new filename
        renderer_text2 = gtk.CellRendererText()
        column_text2 = gtk.TreeViewColumn("Filesize", renderer_text2, text=1)
        column_text2.set_sizing(gtk.TreeViewColumnSizing.AUTOSIZE)
        column_text2.set_resizable(True)
        # Create a TreeViewColumn for the checkbox
        renderer_toggle = gtk.CellRendererToggle()
        renderer_toggle.connect("toggled", self.on_cell_toggled)
        column_toggle = gtk.TreeViewColumn("Check", renderer_toggle, active=2)
        column_toggle.set_sizing(gtk.TreeViewColumnSizing.AUTOSIZE)
        column_toggle.set_resizable(True)
        # Create a TreeView and add the columns
        tree_grid = self.builder.get_object("tree_grid")
        tree_grid.append_column(column_text1)
        tree_grid.append_column(column_text2)
        tree_grid.append_column(column_toggle)
        #
        self.populate_model(sys.path[0])

    def on_cell_toggled(self, widget, path):
        liststore1 = self.builder.get_object("liststore1")
        liststore1[path][2] = not liststore1[path][2]

    def populate_model(self, folder_path):
        liststore1 = self.builder.get_object("liststore1")
        for filename in os.listdir(folder_path):
            filesize = os.path.getsize(os.path.join(folder_path, filename))
            liststore1.append([str(filename), str(filesize), True])


if __name__ == '__main__':
    main = Main()
    gtk.main()

Example_Glade.py (after launching it)

Example_Glade.py (after launching it, then manually expanding the window)

My question: In the "Example_Glade.py + Example_Glade.glade" example, why does the ScrolledWindow not expand to full display the contents of the TreeView widget? What do I need to do to correct this?

I realize that I can remove the ScrolledWindow and just have the TreeView in my Glade example, which would then full display the TreeView. While this is almost what I want, I would still like to have a ScrolledWindow in order to get scrollbars in case there is a lot of data or the filenames are really large and would scroll off the screen. Ultimately, this was just an example, and I'm looking to create something with more columns and data which may necessitate a horizontal and vertical scrollbars.

Any suggestions on what I'm doing wrong in Glade, or if there is a way to set the ScrolledWindow size based on the fully displayed TreeView size, or any other solutions would be most appreciated.


Solution

  • Try to use set_min_content_height. Also read the docs: "Note that this can and (usually will) be smaller than the minimum size of the content". If that doesn't help, you might need to go with set_size_request to the scrolled window.