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.

New contributor

bsfema is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật