代码之家  ›  专栏  ›  技术社区  ›  ceving

如何在Gtk3::TreeViewColumn中设置背景色?

  •  0
  • ceving  · 技术社区  · 4 年前

    Gtk3::TreeViewColumn :

    my $red = Gtk3::Gdk::RGBA->new(1, 0.8, 0.8, 1);
    my $renderer = Gtk3::CellRendererText->new;
    my $column = Gtk3::TreeViewColumn->new_with_attributes
        ($c, $renderer, text => $n, background => $red);
    

    Gtk-CRITICAL **: 09:54:09.834: gtk_list_store_get_value: assertion 'column < priv->n_columns' failed
    GLib-GObject-CRITICAL **: g_value_transform: assertion 'G_IS_VALUE (src_value)' failed at /usr/share/perl5/Gtk3.pm line 568.
    GLib-GObject-WARNING **: unable to set property 'background' of type 'gchararray' from value of type '(null)' at /usr/share/perl5/Gtk3.pm line 568.
    

    以这种方式创建颜色时,会出现相同的错误:

    my $red2 = Gtk3::Gdk::RGBA::parse ('#FF0000');
    

    有人能解释一下如何设置背景色吗?

    #! /usr/bin/perl
    use strict;
    use warnings;
    use utf8;
    
    use Gtk3 -init;
    use Glib 'TRUE', 'FALSE';
    
    my @COLUMNS = (
      'a',
      'b',
      'c');
    
    my $red = Gtk3::Gdk::RGBA->new(1, 0, 0, 1);
    my $red2 = Gtk3::Gdk::RGBA::parse ('#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 scalar @COLUMNS);
    
    my $row = $model->append ();
    $model->set ($row,
                 0, "a",
                 1, "b",
                 2, "c");
    
    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 => $red);
      $treeview->append_column($column);
    }
    
    $window->show_all;
    Gtk3->main();
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Håkon Hægland    4 年前

    问题是这条线:

    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

        2
  •  0
  •   marc_s Anurag    4 年前

    RGBA表示4个值,称为红、绿、蓝、ALPHA(别名透明度)。

    现在再拨一次你的电话,把电话打好!

    my$red2=Gtk3::Gdk::RGBA::parse('#FF000000');