自动调整Python Gtk3 TreeViewColumn,但可以调整它们的大小

时间:2016-12-02 16:10:11

标签: python gtk3 gtktreeview

我尝试更改列的宽度。当我将它们设置为自动调整大小时,但我不能让它们变小。

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

那么也许可以从autosize列宽开始,但也可以在以后更改宽度?

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

上找到
  

如果resizable为True,则列的大小调整模式为   Gtk.TreeViewColumnSizing.AUTOSIZE,然后将大小调整模式更改为   Gtk.TreeViewColumnSizing.GROW_ONLY。

1 个答案:

答案 0 :(得分:1)

试试这段代码:

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

编辑:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys


class GUI:
    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_size_request(400, 200)
        mainbox = Gtk.VBox()
        window.add(mainbox)
        window.connect('destroy', self.on_window_destroy)

        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 longer Text with much much more more content", "A short Text"])
        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)

            column.set_min_width(50)
            column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
            column.set_resizable(True)
            column.set_reorderable(True)
            treeview.append_column(column)
        window.show_all()

    def on_window_destroy(self, window):
        Gtk.main_quit()
def main():
   app = GUI()
   Gtk.main()

if __name__ == "__main__":
   sys.exit(main())

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