Python Gtk3 TreeView TreeViewColumn 用省略号截断文本?

Python Gtk3 TreeView TreeViewColumn truncating text with an ellipsis?

我有 Gtk.TreeView with a Gtk.ListStore 列。

给定任意大小的 window,可由用户更改,具有可调整大小的“名称”列,而所有其他列固定大小 ~

我想要发生的是,当一个超宽的名称字符串被插入到列表中时,它的文本被截断,最好是添加省略号“...”。当 window 重新调整大小时,额外的周长允许显示更多名称。但在任何时候都不需要水平滚动。

现在发生的情况是,超宽文本只是将其他列推到右边。这看起来很愚蠢。如果我阻止水平滚动,父级 window 宽度会自动增加 - 在 已经全屏 windows.

上看起来不太好看

我不想为名称使用 简单 固定像素宽度的列,因为真正的对话需要根据其父级调整大小,其大小为用户的屏幕。所以固定的像素大小几乎总是错误的。

我想要某种不会激活 h-scroll 的解决方案。

任何类似的东西:

到目前为止,我已经尝试了关于 SO 和许多其他地方的几乎所有建议。总能找到非 python 文档和不同的 Gtk 版本的结合确实妨碍了理解。参考:Python GTK3 Doco

编辑: 找到了 Gtk.TreeViewColumn.set_expand() 函数。所以现在名称列扩展为额外的 space.

import sys
import gi

gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf



class ListWindow(Gtk.Window):
    def __init__(self):
        super().__init__(title="List Demo")
        self.set_border_width( 10 )
        self.set_default_size( 400, 300 )

        header = Gtk.HeaderBar(title="List Demo")
        header.props.show_close_button = True
        self.set_titlebar(header)

        self.scrollable = Gtk.ScrolledWindow()
        self.scrollable.set_policy( Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC )

        self.colour_list = Gtk.ListStore( str, int, int, int, GdkPixbuf.Pixbuf )
        self.tree_view = Gtk.TreeView( model=self.colour_list )
        self.tree_view.set_headers_visible( True )

        # For right-justifying columnms
        right_justify = Gtk.CellRendererText()
        right_justify.set_alignment( 1.0, 0.5 )  # right, centre

        # Five columns, name, r, g, b, swatch
        column1 = Gtk.TreeViewColumn( "Name",   Gtk.CellRendererText(), text=0 )
        column2 = Gtk.TreeViewColumn( "Red",    right_justify, text=1 )
        column3 = Gtk.TreeViewColumn( "Grn",    right_justify, text=2 )
        column4 = Gtk.TreeViewColumn( "Blu",    right_justify, text=3 )
        column5 = Gtk.TreeViewColumn( "Swatch", Gtk.CellRendererPixbuf(), pixbuf=4)

        columns = [ column1, column2, column3, column4, column5 ]

        for i,col in enumerate( columns ):
            if ( i == 0 ):
                col.set_sizing( Gtk.TreeViewColumnSizing.AUTOSIZE )  # all columns except first fixed size
                col.set_expand( True )
                col.set_resizable( True )
            else:
                col.set_sizing( Gtk.TreeViewColumnSizing.FIXED )  
                col.set_expand( False )
                col.set_resizable( False )
            self.tree_view.append_column( col )

        self.populateList()

        # FInally add everything to the window
        self.scrollable.add( self.tree_view )
        self.add( self.scrollable )
        self.show_all()


    def getColouredPixmap( self, r, g, b, a=255 ):
        """ Given components, return a colour swatch pixmap """
        CHANNEL_BITS=8
        WIDTH=64
        HEIGHT=32
        swatch = GdkPixbuf.Pixbuf.new( GdkPixbuf.Colorspace.RGB, True, CHANNEL_BITS, WIDTH, HEIGHT ) 
        swatch.fill( (r<<24) | (g<<16) | (b<<8) | a ) # RGBA
        return swatch


    def populateList( self ):
        """ Fill the list with a number of colour-name, components and swatch-image """
        # Probematic Record for the top entry
        swatch = self.getColouredPixmap( 184, 134,  11 )
        #self.colour_list.append( [ 'Sarcoline (literally mea…', 184, 134,  11, swatch ] )
        self.colour_list.append( [ 'Sarcoline (literally meaning: "flesh coloured", so varies...)', 184, 134,  11, swatch ] )

        # A bunch of other records, just for fun
        try:
            fin = open( '/etc/X11/rgb.txt', 'rt' )
            records = fin.readlines()
            fin.close()
        except:
            # excerpt fro those without rgb.txt
            records = """ 245 255 250     MintCream
                          240 255 255     azure
                          240 248 255     alice blue
                          230 230 250     lavender
                          255 240 245     lavender blush """.split( '\n' )

        # 255 240 245     lavender blush
        for rec in records:
            if ( len( rec.strip() ) > 0 and rec[0] != '!' and rec.find('gray') == -1 ):
                rec = rec.strip().replace( '\t', ' ')
                while( rec.find( '  ' ) != -1 ):
                    rec = rec.replace( '  ', ' ' )  # remeove double-spaces
                r,g,b,desc = rec.split( ' ', 3 )
                r = int( r )
                g = int( g )
                b = int( b )
                swatch = self.getColouredPixmap( r, g, b )
                self.colour_list.append( [ desc.capitalize(), r, g, b, swatch ] )


### MAIN
win = ListWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

明确设置 GtkCellRendererText"ellipsize" 属性 似乎有效:

        renderer  = Gtk.CellRendererText()
        renderer.set_property("ellipsize", Pango.EllipsizeMode.END)
        # Five columns, name, r, g, b, swatch
        column1 = Gtk.TreeViewColumn( "Name",   renderer, text=0 )

您可能还对preventing last column from growing

感兴趣