I'm trying to create a simple gtk-perl window in which to display text.

I'm using Ubuntu, on which the default colour scheme is black text on a white background. I want to change those colours.

I've examined the docs and tutorials, and there seem to be two possible approaches. The first is to use tags - this works well, but it only affects the displayed text - not the whole window.

The second approach was demonstrated by zentara (http://www.perlmonks.org/?node_id=634916) . His script successfully changes the colours of button widgets, but when I try to use the same code to change a textview widget, nothing happens.

Here's an example script. Tags work; the displayed text is white on a black background. Some calls to modify_bg() work - the window's scroll bars are green. Other calls to modify_bg() do not work. What I'm trying to achieve is white text on a black background.

#!/usr/bin/perl -w # Short script to demonstrate a GTK ScrollWindow, which has a white ba +ckground # by default; I'd like to use a different colour scheme # # Based on the replies by zentara at # http://www.perlmonks.org/?node_id=636366 # http://www.perlmonks.org/?node_id=633983 BEGIN { $ENV{GTK2_RC_FILES} = 'gtkrc_myapp' } use Gtk2 '-init'; use Gtk2::Helper; use Glib qw(TRUE FALSE); use strict; our $Buffer; our $TextView; # Create the widget &createWindow; # Insert some text into the window on an endless loop for (;;) { &insertText("Hello world!\n"); &insertText("Time to party!\n"); } sub createWindow { # Define colours my $greyh = Gtk2::Gdk::Color->new (0xCCCC,0xCCCC,0xCCCC); my $greyl = Gtk2::Gdk::Color->new (0x9999,0x9999,0x9999); my $redh = Gtk2::Gdk::Color->new (0xFFFF,0,0); my $redl = Gtk2::Gdk::Color->new (0xAAAA,0,0); my $greenh = Gtk2::Gdk::Color->new (0,0xFFFF,0xEEEE); my $greenl = Gtk2::Gdk::Color->new (0,0xFFFF,0xCCCC); my $greend = Gtk2::Gdk::Color->new (0,0xFFFF,0); my $blueh = Gtk2::Gdk::Color->new (0,0xFFFF,0xFFFF); my $bluel = Gtk2::Gdk::Color->new (0,0xCCCC,0xFFFF); # Create a window widget my $windowHandle = Gtk2::Window->new('toplevel'); $windowHandle->signal_connect('delete_event' => sub { exit;}); $windowHandle->set_border_width(0); $windowHandle->set_position('center_always'); $windowHandle->modify_bg ('normal', $greenl); # Create an HBox within the window my $hbox = Gtk2::HBox->new(); $hbox->set( "border_width" => 0 ); $hbox->modify_bg ('normal', $bluel); $windowHandle->add($hbox); # Create a scrolling window my $sw = Gtk2::ScrolledWindow->new; $sw->set_size_request(400,300); $sw->modify_bg ('normal', $redl); # Create a text view $TextView = Gtk2::TextView->new(); $TextView->modify_bg ('normal', $greyl); # Create a text buffer $Buffer = $TextView->get_buffer(); # Create a colour tag for the text buffer $Buffer->create_tag('default', foreground => 'white', background => 'black', ); # Pack the widgets $sw->add($TextView); $hbox->pack_start($sw,1, 1, 1 ); # Put the window widget (and all its children) into the event queu +e $windowHandle->show_all; return 1; } sub insertText { my ($text) = @_; $Buffer->insert_with_tags_by_name($Buffer->get_end_iter, $text,'de +fault'); Gtk2->main_iteration while Gtk2->events_pending; $TextView->scroll_to_iter($Buffer->get_end_iter,0,0,0,0); + return 1; }

In reply to GTK widgets - set colors by ronlewis

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.