如何在treeview gtk3中阻止最后一列的大小

时间:2016-03-17 15:07:01

标签: python-2.7 treeview gtk3

在演示树视图下方。我想修复最后一列的宽度。

经过不同命令的大量测试后,我会求助。

https://andrewsteele.me.uk/learngtk.org/tutorials/python_gtk3_tutorial/html/treeviewcolumn.html

它说:根据内容的变化,也可以通过多种方式定制列的大小:

treeviewcolumn.set_sizing(施胶)

可以将大小参数设置为Gtk.TreeViewColumnSizing.GROW_ONLY将列设置为永不收缩,无论内容如何,​​Gtk.TreeViewColumnSizing.AUTOSIZE将列调整为最佳大小,并在每次模型更改时更新,以及Gtk。 TreeViewColumnSizing.FIXED,它将列设置为固定的像素宽度。

使用我的代码,实际上最后一列是占用可用空间

#!/usr/bin/env python3
# -*- coding: ISO-8859-1 -*-
# liststore.py
from gi.repository import Gtk,Gdk

window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())

liststore = Gtk.ListStore(str, int)
liststore.append(["Oranges", 5])
liststore.append(["Apples", 3])
liststore.append(["Bananas", 1])
liststore.append(["Tomatoes", 4])
liststore.append(["Cucumber", 1])
liststore.append(["potatoes", 10])
liststore.append(["apricot", 100])

treeview = Gtk.TreeView(model=liststore)
treeview.set_rules_hint( True )
window.add(treeview)

treeviewcolumn = Gtk.TreeViewColumn("Item")

treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)

treeviewcolumn = Gtk.TreeViewColumn("Quantity")
treeviewcolumn.props.sizing = Gtk.TreeViewColumnSizing.FIXED


treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
css_provider = Gtk.CssProvider()
css = """
                /* font operate on entire GtkTreeView not for selected row */
                GtkTreeView {
                    text-shadow: 1px 1px 2px black, 0 0 1em blue, 0 0 0.2em blue;
                    color: white;
                    font: 1.5em Georgia, "Bitstream Charter", "URW Bookman L", "Century Schoolbook L", serif;
                    font-weight: bold;
                    font-style: italic;box-shadow: 5px 3px red;}
                GtkTreeView row:nth-child(even) {
                background-image: -gtk-gradient (linear,
                                                   left top,
                   left bottom,
                   from (#d0e4f7),
                   color-stop (0.5, darker (#d0e4f7)),
                   to (#fdffff));
                }
                GtkTreeView row:nth-child(odd) {
                background-image: -gtk-gradient (linear,
                                                   left top,
                   left bottom,
                   from (yellow),
                   color-stop (0.5, darker (yellow)),
                   to (#fdffff));
                }
                /* next line only border action operate */
                GtkTreeView:selected{color: white; background: green; border-width: 1px; border-color: black;}
                /* next line for Gtk.TreeViewColumn */
                column-header .button{color: white; background: purple;}

            """
css_provider.load_from_data(css)
screen = Gdk.Screen.get_default()
style_context = window.get_style_context()
style_context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

window.show_all()
Gtk.main()

1 个答案:

答案 0 :(得分:2)

在您的情况下,问题是GTK尝试以某种方式填充空间,因为没有任何列设置为扩展最后一列将扩展。因此,为了解决您的问题,请更改您的代码,如下所示:

treeviewcolumn = Gtk.TreeViewColumn("Item")
treeviewcolumn.set_expand(True)
treeview.append_column(treeviewcolumn)

这将告诉GTK您希望“项目”列展开,默认情况下此值为False,因此其他列(如“数量”列)将不会展开。