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

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; }

Replies are listed 'Best First'.
Re: GTK widgets - set colors
by halfcountplus (Hermit) on Mar 26, 2011 at 13:43 UTC

    I do gtk programming in C/C++, and generally I respect the principle that the color scheme is supposed to be determined by the user. Sometimes I do allow for a text area background to be configurable, tho.

    It looks to me like you are trying to use text buffer tags to do this. I dunno how well that works, but it obviously won't create a uniform effect. You want to affect the text view which displays the buffer. From your code:

    $TextView->modify_bg ('normal', $greyl);

    Does this not do anything? I am sure that is where you would set a black background.

    My other comment is that depending on how specific what you want to do is, a better way might be to create a gtk theme (.gtkrc) with the colors you want and then set that via "gtk-settings" (this should be part of the perl API somewhere...). That's less useful if you wanted to set values for specific buttons, however.

    BTW, the ubuntu default is not carved in stone, you can set your own default scheme from the command line:

    gtk-chtheme

      Hi guys,

      Thanks for your answers. My script is a telnet client that displays ANSI colours. Most users are going to expect a black background for that (since it's been the default telnet colour scheme since the dawn of time). That's why, on this rare occasion, I don't want to respect the user's choice of desktop theme.

      (The test script changes the colours of several widgets - I hope it's clear that I was trying to demonstrate that ->modify_bg works for most of the widgets, but not for textview.)

      $TextView->modify_bg ('normal', $greyl);

      This really does have no effect on the textview's background colour, at least not on my Ubuntu box, even if I change it to

      $TextView->modify_bg ('normal', $redl);
Re: GTK widgets - set colors
by zentara (Cardinal) on Mar 27, 2011 at 14:34 UTC
    It gets complicated with Gtk2, because it uses a basic system style file in ./~gtkrc-2.0 , further complicated by Ubuntu's advanced theme, which probably uses pixmaps, which are hard to override. Google for all the gory gtkrc-2.0 details. It does get complex, and you can spend days trying to figure out the themes.

    The gtkrc-2.0 file is hard to override for some widgets. See Setting widget bg color in Gtk2

    Here is a general purpose override for the gtkrc style file for the Text box. Notice the color of the cursor needs special handling.

    #!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; Gtk2::Rc->parse_string(<<__); style "my_text" { font_name ="sans 24" text[NORMAL] = "#FFAA00" base[NORMAL] = "#000000" GtkTextView::cursor-color = "red" } style "my_cursor"{ fg[NORMAL] = "#FF0000" } widget "*Text*" style "my_text" __ my $window = Gtk2::Window->new('toplevel'); $window->set_title('Z'); $window ->signal_connect( 'destroy' => \&delete_event ); $window->set_border_width(10); $window->set_size_request(400,400); my $vbox = Gtk2::VBox->new( FALSE, 6 ); $window->add($vbox); $vbox->set_border_width(2); my $hbox= Gtk2::HBox->new( FALSE, 6 ); $vbox->pack_end($hbox,FALSE,FALSE,0); my $ebutton = Gtk2::Button->new_from_stock('gtk-quit'); $hbox->pack_end( $ebutton, FALSE, FALSE, 0 ); $ebutton->signal_connect( clicked => \&delete_event ); # Create a textbuffer to contain that string my $textbuffer = Gtk2::TextBuffer->new(); $textbuffer->set_text('yadda yadda yadda'); # Create a textview using that textbuffer my $textview = Gtk2::TextView->new_with_buffer($textbuffer); # Add the textview to a scrolledwindow my $scrolledwindow = Gtk2::ScrolledWindow->new( undef, undef ); $scrolledwindow->add($textview); $vbox->pack_start($scrolledwindow, 1, 1, 0 ); $window->show_all(); Gtk2->main; ##################################### sub delete_event { Gtk2->main_quit; return FALSE; } #######################################

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      zentara, Khen1950fx, halfcountplus,

      Thanks for your generous help. The code you offered does the job very nicely, and I've learned a thing or two about GTK from your posts.

      Ron

Re: GTK widgets - set colors
by Khen1950fx (Canon) on Mar 27, 2011 at 04:17 UTC
    Why the infinite loop? You can use Devel::throttle to put it into slow motion. On your shebang line:
    #!/usr/bin/perl -d:throttle(0.2)