简体   繁体   中英

How to set a Gtk2::Button's text colour?

I am trying to set a colour to highlight a button. However, the modify_fg method only seems to set the focus ring's colour. modify_bg works as expected.

This is my code:

use Gtk2 qw/-init/;

my $window = Gtk2::Window->new;
$window->set_title("Window!");

my $button = Gtk2::Button->new("Coloured _button");
# does not affect text
$button->modify_fg(normal => Gtk2::Gdk::Color->new(0xffff, 0, 0));

$window->add($button);
$window->show_all;

Gtk2->main;

I'm also interested whether there is a standard colour for this sort of highlight, to blend in with the user's theme.

you can get the button's child label and modify its foreground, below is an example (python, let me know if there are troubles converting it to perl)

import gtk        

class TestWindow:
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        box = gtk.VBox()

        button0 = gtk.Button("Test Button")
        label0 = button0.get_children()[0]
        label0.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('red'))

        button1 = gtk.Button(stock=gtk.STOCK_ABOUT)
        alignment = button1.get_children()[0]
        hbox = alignment.get_children()[0]
        image, label1 = hbox.get_children()
        label1.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('blue'))

        box.add(button0)
        box.add(button1)

        window.add(box)
        window.set_size_request(200, 200)
        window.show_all()        

    def close_application(self, widget, event, data=None):
        gtk.main_quit()
        return False

if __name__ == "__main__":
    TestWindow()
    gtk.main()

hope this helps, regards

All previous helped a lot...

All the following works ok!

my $beige = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xCCCC);
my $azul  = Gtk2::Gdk::Color->new (0,0,0xCCCC);
my $rojo  = Gtk2::Gdk::Color->new (0xCCCC,0,0);

$mw->modify_bg ("normal", $beige);

my $butOk = Gtk2::Button->new_with_label(" 🗹 ¡Ya Terminé!");
($butOk->get_children)[0]->modify_fg("normal", $azul);
($butOk->get_children)[0]->modify_font(Pango::FontDescription->from_string ("Serif Bold 27"));
my $imgBTN = Gtk2::Button->new();
my $img    = Gtk2::Image->new_from_file("$imagen");
$imgBTN->set_property("image"=>$img);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM