Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

add color to a Gtk2::TextView

by navalned (Sexton)
on Sep 29, 2018 at 21:47 UTC ( #1223296=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to change the background color of a TextView widget. Here is one example of what I have tried.

my $w = Gtk2::Window->new(q/toplevel/); my $color = Gtk2::Gdk::Color->parse(q/yellow/); my $eb = Gtk2::EventBox->new(); my $sw = Gtk2::ScrolledWindow->new(undef, undef); my $tv = Gtk2::TextView->new(); $eb->add($tv); $eb->modify_bg(q/normal/, $color); $sw->add_with_viewport($eb); $w->add($sw); $w->show_all(); Gtks->main(); 0;

This doesn't work. I'm able to make the $sw yellow, but as soon as i add the $tv it isn't. Any ideas? Thanks, Edgar

Replies are listed 'Best First'.
Re: add color to a Gtk2::TextView
by zentara (Archbishop) on Sep 30, 2018 at 17:16 UTC
    Hi, here are a few more techniques to add color and images. Setting the cursor color is useful depending on your base color. You will need to provide a background.png file, or comment out the lines relating to it. The Rc->parse_string is needed to override any default colors and images in the ~/.grtkrc-2.0 file.
    #!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; Gtk2::Rc->parse_string(<<__); pixmap_path '.' 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" style "my-scrolledwindow" { xthickness = 10 ythickness = 10 GtkScrolledWindow ::scrollbars-within-bevel = 1 GtkScrolledWindow ::scrollbar-spacing = 10 engine "pixmap" { image { function = SHADOW file = "./background.png" border = {10, 10, 10, 10} detail = "scrolled_window" } } } class "GtkScrolledWindow" style "my-scrolledwindow" __ 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(300,300); my $back_pixbuf = Gtk2::Gdk::Pixbuf->new_from_file('background.png'); my ($pixmap,$mask) = $back_pixbuf->render_pixmap_and_mask(255); my $style = $window->get_style(); $style=$style->copy(); $style->bg_pixmap("normal",$pixmap); $window->set_style($style); my $vbox = Gtk2::VBox->new( FALSE, 6 ); $window->add($vbox); $vbox->set_border_width(5); 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 ); my $str; my $file = shift || $0; open( TEST, "< $file" ) or die "Cannot open test.txt\n"; while (<TEST>) { $str .= $_; } close TEST; # Create a textbuffer to contain that string my $textbuffer = Gtk2::TextBuffer->new(); $textbuffer->set_text($str); # 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); $scrolledwindow->set_shadow_type ('etched-out'); $scrolledwindow->set_policy ('automatic', 'automatic'); $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. ..... an animated JAPH
Re: add color to a Gtk2::TextView
by Anonymous Monk on Sep 30, 2018 at 01:55 UTC
    Try modify_base

      SMH

      I can't believe it was that simple.

      Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1223296]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2022-01-16 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (49 votes). Check out past polls.

    Notices?