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();

In reply to Gtk3 and CSS styles by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.