Hi, your code runs, and the problem is as you describe. However, I think your problem stems from trying to use the Form manager to place multiple canvases on top of another base canvas. I may be wrong, but this is what I was taught. Forming or packing a canvas, on top of another canvas, does not make it an
item of the underlying canvas.
When you try to put another widget into another widget, as opposed to putting them in a container widget, like a frame or window, you need to use the createWindow method of the underlying Canvas. Here is a simple example.
By the way, what sort of things are you putting in your sub-canvases? Why can't you just use a small image, and bind to it for your mouse actions? Also, buttons can be given background images, with Tk::Compound
A second example following, shows how to hide createWindow items.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::JPEG;
use Image::Magick;
my $im = Image::Magick->new;
my $image = shift || 'zen16.jpg'; #roughly a 500x500 jpg
my ($width, $height, $size, $format) = $im->Ping($image);
my $mw = MainWindow->new();
$mw->fontCreate('big',
-family=>'courier',
-weight=>'bold',
-size=>int(-18*18/14));
my $canv = $mw->Canvas(
-bg => 'lightsteelblue',
-relief => 'sunken',
-width => $width,
-height => $height)->pack(-expand => 1, -fill => 'both');
my $img = $mw->Photo(-file => $image );
$canv->createImage( 0, 0, -image => $img, -anchor => 'nw' );
my $text = $mw->Scrolled('Text',
-bg=>'lightyellow',
-scrollbars=>'osoe',
);
my $textcontainer = $canv->createWindow(
$width/2, $height/2,
-window => $text,
-width => $width -200,
-height => $height-200,
-state => 'normal');
my $button = $mw->Button(
-bg=>'lightblue',
-text=>'Push Me',
-command=> sub{ $text->insert('end',time."\n")},
);
my $butcontainer = $canv->createWindow(
125, 25,
-window => $button,
-width => 125,
-height => 25,
-state => 'normal');
my $ctext = $canv->createText( 300,20,
-font=>'big',
-fill=> 'white',
-text=>'la di da');
bunchOfBoxes();
$text->focus;
MainLoop();
sub bunchOfBoxes
{
for(0..10){
my $window = $canv->Checkbutton(-text=> $_);
$canv->createWindow(450, 20 + ($_ * 20),
-window=> $window);
}
}
Showing hiding of a createWindow item
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::JPEG;
use Tk::PNG;
#demonstrates need for subwidget method
#on Scrolled Canvas, to use lower or raise
my $mw = new MainWindow;
my $canvas = $mw->Scrolled('Canvas',
-bg => 'white',
-xscrollincrement => 1,
-yscrollincrement => 1,
-confine => 1,
-scrollbars => 'se',
-width => 200,
-height => 200,
-closeenough =>3,
-scrollregion => [ 0, 0, 500, 500 ],
)->pack(qw/ -fill both -expand 1 -side top/);
my $realcanvas = $canvas->Subwidget('scrolled');
$mw->Button(-text=>"Raise Bunny",
-command => sub{
# $canvas->lower( 'bunny' ,'tux' ); # will cause error
# need subwidget of the scrolled canvas
$realcanvas->raise( 'bunny' ,'background' );
})->pack();
$mw->Button(-text=>"Lower Bunny",
-command => sub{
$realcanvas->lower( 'bunny' ,'background' );
})->pack();
my $rect = $canvas->createRectangle(0,0,500, 500,
-fill => 'orange',
-tags => ['background'],
);
my $bunny = $mw->Photo(-data => get_bunny() );
$canvas->createImage( 40, 40, -image => $bunny,
-anchor => 'nw',
-tags => ['bunny'],
);
my $window = $canvas->Checkbutton(-text=> 'Foo');
my $win = $canvas->createWindow(20,20,
-window=> $window,
);
$canvas->move($win,-1000,-1000); #dx, dy
$mw->Button(-text=>"Show Checkbox",
-command => sub{
$canvas->move($win, 1000,1000 );
})->pack();
$mw->Button(-text=>"Hide Checkbox",
-command => sub{
$canvas->move($win, -1000,-1000 );
})->pack();
# $canvas->lower( 'bunny' ,'background' ); # will cause error
# need subwidget
$realcanvas->lower( 'bunny' ,'background' );
MainLoop;
sub get_bunny{
return
'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAjEAIAAABcJvHFAAAACXBIWXMAAAsSAAALEgHS3
+X78AAAD
F0lEQVR42u1YL+yqUBj1vfcLbhY3C44is8BIREYSG9FoNBqNkok2aFhp2BhJDWyadCZN/i
+lOGxan
jRdOuRsPxl/f+23vJKfX7x6+73znu5dK5RviV9QPDMMwDIPP7/f7/X6XTWU0Go1Go06n0+
+l0PM/z
PC91CNu2bduWZVmW5bLpjsfj8XgcBEEQBJPJZDKZZAw0n8/n8zkCGYZhGIYgCIIgFEt3OB
+wOh8OA
gKZpmqZlDDedTqfTKRnO933f95GVer1er9fz0BVFURRFxCR3QfyMQfv9fr/fDyLgOI7jON
+mo419k
JUkMBoPBYJCRNBrxdrvdbrco6qvVarVaIWdFpQO/5tIcFBbE4nQ6nU6nJIpHjlGlEklTFE
+VRFDIa
T32/3+/3+3jqHMdxHBcfB2sK6HFFURRFeb1er9crfksoNUrr0GvUfxGfnA+FmX+QALDItG
+LDA6O2
pQyCJFkPqxMDK2p9LodOAhQaLRjfoKRGo2wObl3G8PoDsA0Gb5Q5oonjfSNKTh96AOh+u9
+1ut1uS
FuZrONPJ7bJ06tA9TDDsD6QkCnDltEDRkV1Q9AnENyuk8hcyChkkcZKo5uv1er1er3S6cA
+PkFXSx
MQodPrXFg2zTEsVANhO2JNdEmVo80ub7K/lSDHPyLkNaXrVarVar2W46LMuyLFsKaZ7neZ
+4nvwFR
NGKeGjYajUajkXz9z+RLn8/n8/ms/ANIQXq5XC6Xy/v9fr/fvw3p9Xq9Xq9VVVVV9fF4PB
+6Pokhc
r9fr9Vr6s6Lf4dNpbS6/exQA3BHDt/fkPl3wwT85wlcEcrCHZyHO1tmOSl95iGLcQN80Td
+M0jTa1
LMuyLF3XdV03TdM0zWaz2Ww2Xdd1XRenDlDHgTbtvj/ykMZpDm/6LpfL5XLBmGi32+12G6
+Th5RAA
Pne73W63iwfGYFosFovF4kOZrtVqtVoN16TD4XA4HPAAKDp5yZUkSZIk1GGz2Ww2m91ut9
+vt0Mof
lcfxeDwej7PZbDaboRFbrVar1SJfIsLdYZfn8/l8Pue3y1zyiH9VAMFElb5Yp/+PcvAbH/
+25ox5S
PYYAAAAASUVORK5CYII=';
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.