Discussion:
Perl Tk Error
(too old to reply)
Anoop Nagesh
2015-07-21 09:51:59 UTC
Permalink
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
2015-07-21 15:55:46 UTC
Permalink
What line number? What is the actual, complete message?
Post by Anoop Nagesh
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();
$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;
--
Replace "usenet" with "marc" in the e-mail address.
$Bill
2015-07-22 01:04:38 UTC
Permalink
Post by Anoop Nagesh
use Tk;
my $mw = new MainWindow(-title => 'TEST');
$mw->geometry("500x500");
my $canvas = $mw1->Canvas(
-width => 350,
-height => 350,
-background=>'white')
->pack();
$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;
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
2015-07-24 18:52:10 UTC
Permalink
Post by $Bill
Post by Anoop Nagesh
use Tk;
my $mw = new MainWindow(-title => 'TEST');
$mw->geometry("500x500");
my $canvas = $mw1->Canvas(
-width => 350,
-height => 350,
-background=>'white')
->pack();
$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;
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 $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++) {
# I changed it to $x+1 and $lp < $len - not sure if that's what's wanted, but no err.
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;
}
Post by $Bill
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__
Loading...