自动调整Python Gtk3 TreeViewColumn的大小,但使其大小可调整

2024-03-28 09:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图改变列的宽度。当我将它们设置为自动调整大小时,有一个很好的尺寸,但我不能使它们变小。在

然后我将列设置为固定大小,但现在水平滚动条不再显示。在

所以也许可以从自动调整列宽开始,但也可以在以后更改列宽?在

#!/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()

编辑: 在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.


Tags: truegtkcolumnwindowtransactionstreeviewsetstr
1条回答
网友
1楼 · 发布于 2024-03-28 09:44:38

请尝试以下代码:

if True:
    column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
    #column.set_fixed_width(50)

编辑:

^{pr2}$

您仍然无法调整最后一列的大小,但可以调整其他列的大小。希望这有帮助。在

相关问题 更多 >