GTK3 + Glade 不会自动展开 ScrolledWindow 以显示 TreeView 的完整内容

问题描述 投票:0回答:1

我的示例代码:https://github.com/BSFEMA/GTK_Help

这里有 2 个程序做同样的事情。

#1:Example_GPT.py

这是 ChatGPT 编写的示例,旨在帮助我入门。这有效并且 TreeView 完全显示。

#2:Example_Glade.py + Example_Glade.glade

这是我使用 Glade 作为 UI 编写的。这可行,但 ScrolledWindow 未展开以完全显示 TreeView 小部件的内容。

示例_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>

示例_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(启动后)

Example_Glade.py(启动后,然后手动扩展窗口)

我的问题:在“Example_Glade.py + Example_Glade.glade”示例中,为什么 ScrolledWindow 没有展开以完全显示 TreeView 小部件的内容? 我需要做什么来纠正这个问题?

我意识到我可以删除 ScrolledWindow,只在我的 Glade 示例中保留 TreeView,然后它会完全显示 TreeView。 虽然这几乎是我想要的,但我仍然希望有一个 ScrolledWindow 以便获得滚动条,以防有大量数据或文件名非常大并且会滚动到屏幕之外。 最终,这只是一个示例,我希望创建具有更多列和数据的内容,这可能需要水平和垂直滚动条。

任何关于我在 Glade 中做错的事情的建议,或者是否有一种方法可以根据完全显示的 TreeView 大小设置 ScrolledWindow 大小,或者任何其他解决方案,将不胜感激。

python-3.x linux gtk3 glade
1个回答
0
投票

尝试使用set_min_content_height。另请阅读文档:“请注意,这可以并且(通常会)小于内容的最小大小”。如果这没有帮助,您可能需要使用 set_size_request 来滚动窗口。

© www.soinside.com 2019 - 2024. All rights reserved.