For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > October 2007 > Configuring Canvas Object from outside









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Configuring Canvas Object from outside
trisha.woods@gmail.com

2007-10-14, 7:06 pm

Hi All,
I have a main program that calls a function to create a canvas.
The canvas is basically a rectangle. When then canvas pops up, the
rectangle would have grey color fill. The code for creating the
rectangle is given below.

In the main program I have a loop which executes some commands. Based
on the status of these executed commands I need to set the color of
the rectangle to either green or red. I tried writing two functions
set_pass() and set_fail(). The problem is that, once the canvas is
created I am not able to change the color of the rectangle. Can you
please guide me on how can I set the color of the rectangle based on
the status?


(Please note that the code given below does'nt do what it is supposed
to.)


#!/usr/bin/perl

use strict;
use warnings;
use Tk;


# Main Program
my $canvas_ref = "";
my $rect_ref = "";
&create_canvas();

foreach my $i (0..4) {
if ($i%2 == 0) {
&set_pass();
}
else {
&set_fail();
}# else
sleep(2);
}

# Create canvas
sub create_canvas {
my $RECT_W = 100;
my $RECT_H = 100;

my $mw = MainWindow->new();
$mw->title("STATUS");
$mw->geometry('+0+0');

my $cc = $mw->Scrolled("Canvas")->pack();
my $canvas = $cc->Subwidget("canvas");

my $rect = $cc->createRectangle(10, 10, $RECT_W+10, $RECT_H+10 , -
fill => 'grey');

$cc->configure(-scrollregion => [ $canvas->bbox ("all") ],
-confine => 1,
-width => 110,
-height => 110,
);

$rect_ref = $rect;
$canvas_ref = $cc;
MainLoop();
}# sub

sub set_pass {
$canvas_ref -> itemconfigure($rect_ref, -fill => 'green');
}# sub

sub set_fail {
$canvas_ref -> itemconfigure($rect_ref, -fill => 'red');
}# sub

Lamprecht

2007-10-14, 7:06 pm

trisha.woods@gmail.com schrieb:
> Hi All,
> I have a main program that calls a function to create a canvas.
> The canvas is basically a rectangle. When then canvas pops up, the
> rectangle would have grey color fill. The code for creating the
> rectangle is given below.
>
> In the main program I have a loop which executes some commands. Based
> on the status of these executed commands I need to set the color of
> the rectangle to either green or red. I tried writing two functions
> set_pass() and set_fail(). The problem is that, once the canvas is
> created I am not able to change the color of the rectangle. Can you
> please guide me on how can I set the color of the rectangle based on
> the status?


Hi,
you will have to either execute your commands in a callback (Take a look
at Tk::after for timers and repeated callbacks) or call $mw->update in
between your calculations to make your itemconfigure settings take
effect. Also in the latter case you should not use MainLoop, because
that will not return until your MainWindow is destroyed.


Christoph

>
>
> (Please note that the code given below does'nt do what it is supposed
> to.)
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use Tk;
>
>

my $mw;
> # Main Program
> my $canvas_ref = "";
> my $rect_ref = "";
> &create_canvas();
>
> foreach my $i (0..4) {
> if ($i%2 == 0) {
> &set_pass();
> }
> else {
> &set_fail();
> }# else
> sleep(2);

$mw->update;
> }
>
> # Create canvas
> sub create_canvas {
> my $RECT_W = 100;
> my $RECT_H = 100;
>
> my $mw = MainWindow->new();
> $mw->title("STATUS");
> $mw->geometry('+0+0');
>
> my $cc = $mw->Scrolled("Canvas")->pack();
> my $canvas = $cc->Subwidget("canvas");
>
> my $rect = $cc->createRectangle(10, 10, $RECT_W+10, $RECT_H+10 , -
> fill => 'grey');
>
> $cc->configure(-scrollregion => [ $canvas->bbox ("all") ],
> -confine => 1,
> -width => 110,
> -height => 110,
> );
>
> $rect_ref = $rect;
> $canvas_ref = $cc;

# MainLoop();
> }# sub
>
> sub set_pass {
> $canvas_ref -> itemconfigure($rect_ref, -fill => 'green');
> }# sub
>
> sub set_fail {
> $canvas_ref -> itemconfigure($rect_ref, -fill => 'red');
> }# sub
>

trisha.woods@gmail.com

2007-10-15, 7:05 pm

Hi Christoph,
Thanks for your reply. I found the second alternative more useful
and that indeed worked for me. I am calling the subroutine to create
canvas from the main program and not using Mainloop anymore.

thanks a lot for your help.
~Trisha

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com