问题是这条线:
my $column = Gtk3::TreeViewColumn->new_with_attributes
($c, $renderer, text => $n, background => $red);
background
属性需要
Gtk3::ListStore
索引,但您没有给出整数索引,而是提供RGBA对象
$red
Gtk::ListStore
将颜色包含为RGB字符串:
use feature qw(say);
use strict;
use warnings;
use Gtk3 -init;
use Glib 'TRUE', 'FALSE';
my @COLUMNS = qw( a b c );
my $ncols = scalar @COLUMNS;
my $red = "#f00";
my $window = Gtk3::Window->new ('toplevel');
my $notebook = Gtk3::Notebook->new;
$window->add($notebook);
my $sw = Gtk3::ScrolledWindow->new (undef, undef);
$notebook->append_page ($sw, Gtk3::Label->new ("tab"));
my $model = Gtk3::ListStore->new ((('Glib::String') x $ncols), 'Glib::String');
my $row = $model->append ();
$model->set ($row, (map {($_, $COLUMNS[$_])} 0..$#COLUMNS), $ncols, $red);
my $treeview = Gtk3::TreeView->new($model);
$sw->add($treeview);
while (my ($n, $c) = each @COLUMNS) {
my $renderer = Gtk3::CellRendererText->new;
my $column = Gtk3::TreeViewColumn->new_with_attributes
($c, $renderer, text => $n, background => $ncols);
$treeview->append_column($column);
}
$window->signal_connect( destroy => sub { Gtk3->main_quit() } );
$window->show_all;
Gtk3->main();
另请参见
Python 3 and Gtk+3 - issue with TreeView and alternate colors of rows