gpt4 book ai didi

python - GtkTreeView 的限制高度

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:18 24 4
gpt4 key购买 nike

我正在尝试为 GtkTreeView 设置高度限制,如果行数据超过此限制,我想使用滚动条。目前,整个窗口无限制地增长并在某些时候离开屏幕。需要什么才能获得所需的行为?

我的 Python 代码(它只是在每次单击按钮时向列表存储添加一个新行):

#!/usr/bin/env python3

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

class MyApp:

def on_window1_destroy(self, obj, data=None):
Gtk.main_quit()

def button1_clicked(self, object):
liststore = self.builder.get_object("liststore1")
print(self.window.get_default_size())
liststore.append(("test",))

def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("table_test.glade")
self.builder.connect_signals(self)
self.window = self.builder.get_object("window1")
self.window.show()

if __name__ == "__main__":
main = MyApp()
Gtk.main()

对应的Glade文件(test_table.glade):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name column1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="resizable">False</property>
<property name="default_height">300</property>
<signal name="destroy" handler="on_window1_destroy" swapped="no"/>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="button1_clicked" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">test-column</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

最佳答案

您不需要实现该小部件,它已经存在并且名为 Gtk.ScrolledWindow .滚动条行为可以是自动的(仅在内容超出分配的空间时显示)。

这可以通过向您通过 glade 添加的 GtkTreeView 添加父小部件来实现。

如果您不希望按钮随窗口按比例扩展,那么您应该将 GtkButton 垂直对齐设置为 Start(也在 glade 中)。

这是您的 glade 文件,添加了 Gtk.ScrolledWindow :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name column1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_height">300</property>
<signal name="destroy" handler="on_window1_destroy" swapped="no"/>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">start</property>
<signal name="clicked" handler="button1_clicked" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">test-column</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

结果应该是这样的:

enter image description here

关于python - GtkTreeView 的限制高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com