Re: Label font and color



This is in the gtk FAQ. Basically a label widget doesn't have a window
of its own, and therefore it does not have a background that may be
painted. In order to get a background you have to put the label
inside an eventbox.

The following program shows you how to do it.

#!/usr/local/bin/perl
######################################################################
#  Example of how to create a colored grid of entries like in
#  sdd tool.
#
#  This shows that in order to be able to control the background
#  of a label through a style, it must have an eventbox below it.
#  Only then will its background actually be painted...
#
#  Dov Grobgeld <dov imagic weizmann ac il>
#  This script is in the public domain.
######################################################################

use Gtk;

init Gtk;

Gtk::Rc->parse_string(<<__);
style "red" {
        font = "-adobe-helvetica-bold-r-normal--*-120-*-*-*-*-*-*"
        bg[NORMAL] = "#FF0000"
        fg[NORMAL] = "#FFFFFF"
}

style "green" {
        font = "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*"
        bg[NORMAL] = "#00FF00"
        fg[NORMAL] = "#000000"
        text[NORMAL] = "#FF0000"
}

widget "*" style "green"
widget "*label2*" style "red"
widget "*label*2" style "red"

__
  
my $mw = Gtk::Widget->new("Gtk::Window",
                          -type                 => -toplevel,
                          -title                =>      "grid-entries",
                          -allow_grow           =>      1,
                          -allow_shrink         =>      1,
                          -signal::destroy => sub {exit}
                         );

sub table_pack {
    my $table = shift;
    my $child = shift;
    my $row = shift;
    my $clm = shift;
    $table->attach($child, $clm, $clm+1, $row, $row+1, 
                   ['fill', 'expand','shrink'], ['fill', 'expand','shrink'], 0, 0);
}

my $table = new Gtk::Table(5,5,0);
$mw->add($table);

for ($i=0; $i<5; $i++) {
    for ($j=0; $j<5; $j++) {
        my $label = Gtk::Label->new(sprintf("%x", $i*$j%15));
        my $eventbox = Gtk::EventBox->new();
        $eventbox->add($label);
        $label->set_name("label$i$j");
        $eventbox->set_name("label$i$j");
        table_pack($table, $eventbox, $i,$j);
    }
}

$mw->show_all();

main Gtk;

On Sun, Apr 28, 2002 at 11:23:00AM -0500, David J. Trombley wrote:

    Hello!

    I'm wondering if there is an easy way to change the font size/color 
of a Label.  Pango doesn't seem to be supported in Gtk-Perl, and when I 
try to allocate a color in the parent window and set the foreground 
color of the gc of the label, it seems all the other text in the parent 
window shows that color also!   How can I change just the color/size of 
the label text?

    Thanks,
    dj trombley
    <dtrom bumba net>

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]