Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Tk geometry, pixel coordinates precision, canvas and outline

by tybalt89 (Monsignor)
on Apr 23, 2018 at 14:18 UTC ( [id://1213434]=note: print w/replies, xml ) Need Help??


in reply to Tk geometry, pixel coordinates precision, canvas and outline

1. You may be seeing the highlight rectangle. Add -highlightthickness => 0, to your canvas.
(Is the highlightthickness the reason it looks like there is an extra pixel?)

2. the reason you are seeing red background is that createRectangle does not draw two edges.

$canvas->createRectangle(x1, y1, x2, y2, ?option, value, optio +n, value, ...?) The arguments x1, y1, x2, and y2 give the coordinates of two di +agonally opposite corners of the rectangle (the rectangle will include its upper and left edges +but not its lower or right edges).

Does the following do what you want ?

#!/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', -highlightthickness => 0, )->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+50,$sq_y+50, -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"; }

Replies are listed 'Best First'.
Re^2: Tk geometry, pixel coordinates precision, canvas and outline
by Discipulus (Canon) on Apr 24, 2018 at 09:37 UTC
    Thanks master tybalt89 for your insights,
    • 1 yes with -highlightthickness => 0 as in your example everything is looking better. You set this on the main canvas and if I understand affects the frame where rectangles will be drown. But in effect this looks ok until -outline => undef is there. I'm starting to doubt that -outline => undef means a transparent outline: probably it means no outline at all so less pixel used.

    Compare difference if you comment/uncomment -outline => undef in the following snippet:

    use Tk; my $mw = Tk::MainWindow->new(); my $can = $mw->Canvas( -height => 100, -width => 100 , -bg => 'red', -highlightthickness => 0, )->pack( ); $can->createRectangle(0,0,101,101, -fill => 'gold2', #-outline => undef , #-width => 10, ); MainLoop;

    With #-outline => undef , the black, 1px outline is shown in North and West sides but no in other ones. This seems to prove that the outline width is added to. By other hand with -outline => undef no red background in shown so the yellow square fills the correct 100x100 region.

    The weird part is that it seems that just first pixel is managed in this way, ie shifting the yellow square toward SE: infact using -width => 10 (the width of the outline!) the yellow square remains centerd..

    • 2 Thanks i overlooked this part of the docs.. So I must always consider 1 pixel more while drawing rectangles, even with no outline.
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1213434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found