Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl Tk Error

38 views
Skip to first unread message

Anoop Nagesh

unread,
Jul 21, 2015, 5:52:00 AM7/21/15
to
Hi,

while running my perl tk script I got an error like "Bad Screen Distance", please someone helpme regarding this issue. Thanks in advance. Here is a code for reference.


use Tk;

my $mw = new MainWindow(-title => 'TEST');
$mw->geometry("500x500");
my $canvas = $mw1->Canvas(
-width => 350,
-height => 350,
-background=>'white')
->pack();


my @arr1 = (10,100,200,150,250);
my $len = @arr1;

$ver = $canvas->createLine(5,5,5,250,
-width=>4,
-arrow=>'first',
-arrowshape=>[5,5,5]);

$hor = $canvas->createLine(3,250,250,250,
-width=>4,
-arrow=>'last',
-arrowshape=>[5,5,5]);

for (my $lp = 1,my $y=20,my $n=1,my $x=0; $lp <= $len ; $lp++,$y+=10,$n++,$x++ ){

$id = $canvas->createLine($arr1[$x],$y,$arr1[$x+2],$y,
-width=>1,
-arrow=>'both',
-arrowshape=>[3,3,3]);
$tx = $canvas->createText(int(($arr1[$x]+$arr1[$x+2])/2),$y,-text=>"$n");
}

$bt = $mw1->Button(
-text=>'Quit',
-command=>sub {exit})
->pack(
-side=>'bottom');

MainLoop;

Marc Dashevsky

unread,
Jul 21, 2015, 11:55:39 AM7/21/15
to
What line number? What is the actual, complete message?

In article <0a2cfd07-a953-4d96...@googlegroups.com>, anoop...@gmail.com says...
--
Replace "usenet" with "marc" in the e-mail address.

$Bill

unread,
Jul 21, 2015, 9:04:44 PM7/21/15
to
I played around with this a bit and I think I know what's wrong.

use strict;
use warnings;
use Tk;

my $mw = new MainWindow(-title => 'TEST');
$mw->geometry("500x500");
my $canvas = $mw->Canvas(-width => 350, -height => 350,
-background => 'white')->pack();
my @arr1 = (10, 100, 200, 150, 250);
my $len = @arr1;
my $ver = $canvas->createLine(5, 5, 5, 250, -width => 4, -arrow => 'first',
-arrowshape => [5, 5, 5]);
my $hor = $canvas->createLine(3, 250, 250, 250, -width => 4, -arrow => 'last',
-arrowshape => [5, 5, 5]);

my $y = 20; my $n = 1; my $x = 0;
for (my $lp = 1; $lp < $len; $lp++) {

# the x+2 below and the $lp <= $len above caused indexing past the end of @arr1
# I changed it to $x+1 and $lp < $len - not sure if that's what's wanted, but no err.

my $id = $canvas->createLine($arr1[$x], $y, $arr1[$x+1], $y,
-width => 1, -arrow => 'both', -arrowshape => [3, 3, 3]);
my $tx = $canvas->createText(int (($arr1[$x] + $arr1[$x+1]) / 2), $y,
-text => $n);
$y += 10; $n++; $x++;
}
my $bt = $mw->Button(-text => 'Quit', -command => sub { exit; })->pack(
-side => 'bottom');
MainLoop;

__END__

$Bill

unread,
Jul 24, 2015, 2:52:15 PM7/24/15
to
OK, the $lp <= $len is OK - since you're base 1 indexing, but $x still needs to
stay inside the @arr1 bounds. $lp isn't really needed if you use $x for indexing
and $n is simply $x+1. See below:

my $y = 20;
for (my $x = 0; $x < @arr1 - 1; ++$x) { # switched to $x for index and base 0 indexing
my $id = $canvas->createLine($arr1[$x], $y, $arr1[$x+1], $y,
-width => 1, -arrow => 'both', -arrowshape => [3, 3, 3]);
my $tx = $canvas->createText(int (($arr1[$x] + $arr1[$x+1]) / 2), $y,
-text => $x+1);
$y += 10;
0 new messages