Hello monks and nuns!

before the end of the Tk questions months I have another tk question.

My goal is to have a canvas filling the mainwindow and I want to drow squares into this canvas to have evenly divided as a chessboard.

The first question about mainwindow geometry was already answered by the wise kcott in the CB, but I also put it here for completeness and for future readers:

Infact, even using a mainwindow with geometry 99x99 I have undesired pixels here and there: in the program below I try to create 4 squares to fulfill the main canvas (but they do not arrive at the east and south borders: run it to see). Also the pixel used to outline the square are problematic..

Notice that -outline => undef means transparent (you'll see the red of the main big canvas underlying), but specifying no outline assumes black as default .
#!/usr/bin/perl use warnings; use strict; use Tk; # uncomment if module is installed # use Tk::WidgetDump; my ($dx,$dy); my $mw = Tk::MainWindow->new(); # kcot gently confirmed that 100x100 in geometry means # from 0,0 to 100,100 aka 101 pixels!! $mw->geometry("99x99"); # main underlying canvas is filled with RED my $can = $mw->Canvas( -height => 100, -width => 100, -bg => 'red', )->pack( ); # used to dump coordinates my %board; # alternate colors for tales my @alt_col = qw(gold2 green); # squares for tales starting at 0,0 my ($sq_x,$sq_y) = (0,0); foreach my $letter (('A'..'B')){ unshift @alt_col, pop @alt_col; foreach my $num (1..2){ # cycle colors unshift @alt_col, pop @alt_col; $can->createRectangle($sq_x, $sq_y, $sq_x+49,$sq_y+49, -fill => $alt_col[0], # outline undef means ransparent outline # if not specified defaults to black outline -outline => undef , -tags=> ['first'] ); $board{$letter.$num} = {tx=>$sq_x,ty=>$sq_y,bx=>$sq_x+49,by=>$ +sq_y+49}; # add 50 $sq_x += 50; } # reset x to 0 for new row $sq_x = 0; # add 50 to y $sq_y += 50; print "\n"; } # legenda print "ty = top Y tx = top X by = bottom Y bx = bottom X\n"; # dump board coordinates foreach my $key (sort keys %board){ print "$key -> ", ( map{"$_ $board{$key}{$_} "} reverse sort keys %{$board{$key}} ),"\n"; } $can->bind('first', '<1>', sub {&show_click();}); # uncomment if module is installed # $mw->WidgetDump; MainLoop; sub show_click{ my $ev = $can->XEvent; ($dx, $dy) = ($ev->x, $ev->y); print "CLICKED $dx $dy\n"; my $cur_id = ($can->find('withtag','current'))[0]; print "current canvas $cur_id\n\n"; }

What the program outputs seems what I want; ie squares of 50 pixels sides:

ty = top Y tx = top X by = bottom Y bx = bottom X A1 -> ty 0 tx 0 by 49 bx 49 A2 -> ty 0 tx 50 by 49 bx 99 B1 -> ty 50 tx 0 by 99 bx 49 B2 -> ty 50 tx 50 by 99 bx 99

But what is shown on the screen seems misplaced toward north-west by one or more pixels. Also: when I dump the canvas I get:

# main canvas width: 104 height: 99 reqwidth: 104 reqheight: 104 # squares coordinates dumped (from top left to bottom right) are: 0,0,49,49 50,0,99,49 0,50,49,99 50,50,99,99 # that reflect what I want

Thanks for the patience and happy Tk month!

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Tk geometry, pixel coordinates precision, canvas and outline by Discipulus

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.