Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Gtk3 and CSS styles

by Anonymous Monk
on Feb 13, 2019 at 15:31 UTC ( #1229874=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In Gtk2, I could apply a style to various widgets using RC strings. Under Gtk3, it's necessary to use CSS styles.

I can't find any working examples of the new Perl code, besides one from 2012 which no longer works. (The site won't let me post the link.)

I am able to apply a CSS style to a whole window, using the script below. But applying the style to a single widget (in this case, a Gtk3::TextView) does not work, and I can't see why it's wrong.

#!/usr/bin/perl use strict; use diagnostics; use warnings; #use Glib qw(TRUE FALSE); use Gtk3 '-init'; # Set up CSS style my $provider = Gtk3::CssProvider->new(); # Why are these lines necessary? What function do they serve? my $display = Gtk3::Gdk::Display::get_default; my $screen = Gtk3::Gdk::Display::get_default_screen($display); Gtk3::StyleContext::add_provider_for_screen($screen, $provider, 600); # This applies the style to the whole window #my $theming = "* {\nbackground-color: #000000;\ncolor: #FF0000;\n}"; #$provider->load_from_data($theming, -1, undef); # But this doesn't apply the style (just) to the textview my $theming = "GtkTextView {\nbackground-color: #000000;\ncolor: #FF00 +00;\n}"; $provider->load_from_data($theming, -1, undef); # Open a Gtk3 window containing a Gtk3::TextView my $window = Gtk3::Window->new('toplevel'); $window->set_title('Hello world'); $window->set_position('center'); $window->set_default_size(600, 400); $window->signal_connect('delete-event' => sub { Gtk3->main_quit(); exit; }); my $scrollWin = Gtk3::ScrolledWindow->new(undef, undef); $window->add($scrollWin); my $textView = Gtk3::TextView->new; $scrollWin->add_with_viewport($textView); $textView->get_buffer()->set_text("Hello, world!"); $window->show_all(); # Start Gtk3's main loop Gtk3->main();

Replies are listed 'Best First'.
Re: Gtk3 and CSS styles
by holli (Monsignor) on Feb 13, 2019 at 22:44 UTC
    Educated guess: my $theming = ".GtkTextView {\nbackground-color: #000000;\ncolor: #FF0000;\n}";


    You can lead your users to water, but alas, you cannot drown them.

      Thanks to a typo, I stumbled on the answer. Thanks, wobbly fingers!

      my $theming = "textview text {\nbackground-color: #000000;\ncolor: #FF +0000;\n}"; $provider->load_from_data($theming);

      Nope. I tried *TextView as well.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1229874]
Approved by Corion
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2020-02-17 14:00 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (71 votes). Check out past polls.