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

Sponsored Links







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

Copyright 2008 codecomments.com