Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Transparent draw with tk

by traveler (Parson)
on Nov 06, 2002 at 18:28 UTC ( [id://210834]=perlquestion: print w/replies, xml ) Need Help??

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

I want to draw a line on a picture using perl and Tk. Here is what I have. I get to see the line, but not the graphic. If I don't create the canvas (and pack the image instead of placing it) I can see the image. What am I missing?
#! /usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new(-title => "Try to put a line over an image" ) +; $mw -> Photo ('imggif', -file => 'C:/foo.gif'); my $cf = $mw->Frame; $cf->pack( -side => 'left', -fill => 'y', -expand => 1); my $label = $cf->Label( -image => 'imggif'); # $label->pack( -side => 'left', -fill => 'both', -expand => 1); $label->place( -anchor => 'nw', -relheight => 1, -relwidth => 1, -x => 0, -y => 0); my $c = $cf->Scrolled("Canvas", -cursor => 'crosshair', # was $mw-> -scrollbars => 'se') ->pack( -side => 'left', -fill => 'both', -expand => 1); my $line = $c->createLine(0,0,100,100, -width => 2, -fill => "red"); MainLoop;
Thanks! --traveler

Replies are listed 'Best First'.
Re: Transparent draw with tk
by Daruma (Curate) on Nov 07, 2002 at 00:30 UTC
    Greetings!!

    I used your code and tried to force it to do what you describe. Unfortunately, I was unable to get it to work, but I'll keep trying.

    What's happening is that you are creating a Frame widget then packing it, and creating a Canvas widget, then packing it ON TOP of the Frame widget.

    To see this, I added the following, right before the MainLoop();...

    $c->lower();
    This lowers the Canvas widget, $c in the stack of displayed widgets, placing it below the $cf Frame widget.

    It sounds as though you are trying to mix widgets here... The Canvas widget allows you to draw lines, other shapes and create text. The Frame widget allows you to place other widgets, but not draw lines. A big difference between the two, as far as I can tell, is that the Canvas widget only allows you to place a Bitmap object, not other graphical formats, like those allowed by the $mw->Photo() method, like .gif files...

    I'll keep digging around and see if I come up with anything... It's definitely an interesting question...

    -Daruma

    Update: I found a way to do this!! Basically, I put everything into the Canvas widget... And I did find a way to put other image types into a Canvas widget other than bitmaps... the $c->createImage() method works for that...

    Here's the code I used to do this...

    #!c:\perl\bin\perl.exe use strict; use warnings; use diagnostics; use Tk; my $mw = MainWindow->new(-title => "Line over an image"); my $c = $mw->Scrolled("Canvas", -cursor => 'crosshair', -scrollbars => 'se', ) ->pack(-anchor => 'nw', -fill => 'both', -expand => 1, ); my $tile = $c->createImage(50, 50, -image => $mw->Photo(-file => Tk->findINC('Xcamel.gif')), ); my $line = $c->createLine(0, 0, 100, 100, -width => 2, -fill => "red", ); MainLoop();
      You are right. It seems that I cannot create a label with a transparent bg. Putting everything into the canvas seems to work.

      --traveler

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-25 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found