This script below draws a combination of squares and text on a Gnome2::Canvas. I want to guarantee that the text will always be visible above the squares, but the script doesn't work as intended.

I'm using Gnome2::Canvas::Item->lower_to_bottom() and ->raise(). There's a white background, which is placed like this:

    $bg->lower_to_bottom();

Then there are some red squares, which are placed like this:

$square->lower_to_bottom(); $square->raise(1);

Finally, there is some text, which is placed like this:

$text->lower_to_bottom(); $text->raise(2);

Clicking on a red square destroys its canvas object, and replaces it with a (new) yellow square, placed in the same way:

$replaceSquare->lower_to_bottom(); $replaceSquare->raise(1);

Unfortunately, the example script doesn't have the desired effect. Initially, four of the five text labels are drawn beneath their (red) squares. The text is only revealed by clicking on the red square.

Can anyone tell me what I'm doing wrong?

#!/usr/bin/perl package canvas; use strict; use diagnostics; use warnings; use Gtk2 '-init'; use Glib qw(TRUE FALSE); use Gnome2::Canvas; # Canvas size doesn't matter that much, as long as it's a bit bigger t +han the window my $size = 600; my $flipFlag = FALSE; # Create the window my $window = Gtk2::Window->new(); $window->set_title('Canvas'); $window->set_position('center'); $window->set_default_size($size, $size); $window->set_border_width(5); $window->signal_connect (destroy => sub { Gtk2->main_quit; }); # Add a VBox containing a Gnome2::Canvas my $vBox = Gtk2::VBox->new(FALSE, 0); my $canvasFrame = Gtk2::Frame->new(undef); $canvasFrame->set_border_width(3); my $canvasScroller = Gtk2::ScrolledWindow->new(); $canvasScroller->set_border_width(3); $canvasScroller->set_policy('always','always'); my $canvas = Gnome2::Canvas->new(); $canvas->set_scroll_region(0, 0, $size, $size); $canvas->set_center_scroll_region(1); $canvas->set_pixels_per_unit(1); my $canvasRoot = $canvas->root(); $canvasScroller->add($canvas); $canvasFrame->add($canvasScroller); $vBox->pack_start($canvasFrame, TRUE, TRUE, 0); $window->add($vBox); # Draw a white background my $bg = Gnome2::Canvas::Item->new ( $canvasRoot, 'Gnome2::Canvas::Rect', x1 => 0, y1 => 0, x2 => $size, y2 => $size, fill_color => '#FFFFFF', outline_color => '#FFFFFF', ); $bg->lower_to_bottom(); # Draw some squares above the background for (my $count = 0; $count < 5; $count++) { my ($square, $posn, $newColour); $posn = ($count * 100); $square = Gnome2::Canvas::Item->new( $canvasRoot, 'Gnome2::Canvas::Rect', x1 => ($posn + 10), y1 => ($posn + 10), x2 => ($posn + 60), y2 => ($posn + 60), outline_color => '#000000', fill_color => '#FF0000', ); $square->lower_to_bottom(); $square->raise(1); # Click on a red square? Then destroy it, and replace it with a ye +llow one! $square->signal_connect (event => sub { my ($widget, $event) = @_; my $replaceSquare; if ($event->type eq 'button-press') { $square->destroy(); $replaceSquare = Gnome2::Canvas::Item->new( $canvasRoot, 'Gnome2::Canvas::Rect', x1 => ($posn + 10), y1 => ($posn + 10), x2 => ($posn + 60), y2 => ($posn + 60), outline_color => '#000000', fill_color => '#FFFF00', ); $replaceSquare->lower_to_bottom(); $replaceSquare->raise(1); } }); } # Draw some blue text above the squares for (my $count = 0; $count < 5; $count++) { my $text = Gnome2::Canvas::Item->new( $canvasRoot, 'Gnome2::Canvas::Text', x => (($count * 100) + 25), y => (($count * 100) + 25), fill_color => '#0000FF', font => 'Sans', size => 10000, anchor => 'GTK_ANCHOR_W', text => 'Hello world!', ); $text->lower_to_bottom(); $text->raise(2); } # Open the window $window->show_all(); Gtk2->main();

In reply to Gnome2::Canvas - things on top of other things 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.