Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl/Tk grid geometry manager padding things I don't want it to

by JPaul (Hermit)
on Feb 09, 2002 at 02:03 UTC ( [id://144305]=perlquestion: print w/replies, xml ) Need Help??

JPaul has asked for the wisdom of the Perl Monks concerning the following question:

Hey folks,

I've just started learning Perl/Tk (Whoa, is that cool... A GUI? Who'd a thunk it) and am starting by attempting to make a simple old-school RPG like game.
My very first exercise was to draw a couple of tiles (walls/floor/empty space) and then have Perl/Tk stick them into a window to form the floorplan.
After a bit of trial-and-error, I have some semi-working code, but from somewhere there is some padding appearing (about 5 pixels or so) around my tiles, making a rather nice griddle effect, that somewhat breaks up whats supposed to be a continuous floorplan.
The manpage says the padding defaults to 0 (I also specifically told it to pad 0, to no effect) and I can't seem to find anything in the POD, or my Advanced Perl Programming (which only VERY lightly touches on the grid manager) as to explain this padding.
Don't suppose anyone could be a champ and point out what I'm doing wrong, or what I'm not supplying?

my $MW = Tk::MainWindow->new; my (@pl) = qw/-side top -expand yes -padx 10 -pady 10 -fill both/; my $frame = $MW->Frame->pack(@pl); my $canvas = $frame->Canvas(-width => 320, -height => 320)->grid; for(my $x = 0; $x <= 9; $x++) { for(my $y = 0; $y <= 9; $y++) { my $ttile = $MW->Photo(-file => "./tiles/brick.xpm"); my $label = $canvas->Label(-image => $ttile, -height => 32, -width => 32) ->grid(-row => $x, -col => $y); } } $canvas->update; MainLoop;
JP,
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Replies are listed 'Best First'.
Re: Perl/Tk grid geometry manager padding things I don't want it to
by BeernuT (Pilgrim) on Feb 09, 2002 at 02:33 UTC
    I'm not a pro at Tk but from your code that you show and a test with a brick.xpm that I have, it looks to be the pic itself. If you view it in a picture viewer you will see that it has an edge padding also.

    However, i was able to achieve what i beleave is the look your looking for by playing with the width and heigth
    my $label = $canvas->Label(-image => $ttile, -heigth => 19, -width => +17)


    hope this helps

    -bn
      G'day,
      Interesting. I had previously thought of this and checked all my tiles in GIMP to verify there is no border around the images. GIMP also reports each of my tiles is 32x32, which means my height/width SHOULD be exactly right.
      What I don't understand therefore is why shrinking the height/width makes what still appears to be padding disappear.
      If I set the height/width to 28x28, it works quite nicely (Didn't I tell you it was a 5 pixel pad? :) And the tiles don't look terribly smaller.
      Another argument for it not being the tile is that if it WAS the tile image with the padding, making it smaller would only scale the image, not cut out any surrouding padding - so all I'd get would be a tighter/smaller grid pattern!

      Nonetheless, it produces satisfactory results, so I'm mostly happy.
      (Update: The image can be found: Here.)

      But if anyone can explain WHY this happens I'd still be very interested.
      My thanks,

      JP,
      -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Re: Perl/Tk grid geometry manager padding things I don't want it to
by {NULE} (Hermit) on Feb 09, 2002 at 20:07 UTC
    Hi JPaul,

    I'm glad you got your sample code working. I don't have a lot to offer as to why it wasn't work as intended, except to say that I'd be it was an artifact of using Canvas as a parent for (a grid widget that is a parent for) Label widgets. Canvas has it's own methods for handling images (check $canvas->createImage()) that might be easier and more efficient for you. You could just place the image at 32 * $x, 32 * $y then. I think by squishing the images in a smaller Label widget you will create artifacts you have to deal with later.

    Good luck, and if you end up with something playable, I hope you post it. :)

    {NULE}
    --
    http://www.nule.org

      Well heck,
      That was a brilliant suggestion, and sure enough, it works real slick.
      for(my $x = 0; $x <= 9; $x++) { for(my $y = 0; $y <= 9; $y++) { my $ttile = $MW->Photo(-file => "./tiles/brick.xpm"); $canvas->createImage(32 * $x, 32 * $y, -image => $ttile); } }
      Good thinking {NULE} and much appreciated.

      JP,
      -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found