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

Using Perl/Tk, I have a label that I want to use with both text and a picture.
To set the text I use
$text_replacement_res = $wg_name->configure(-text => $new_text);
To set the picture I use
$image_name_full = $image_loc . $new_image; $image_use = $mw_Main->Photo(-file => $image_name_full); $wg_name->configure(-image => $image_use);
where
$wg_name is the name of label widget
$new_text is the new text for the label
$mw_Main is the top level widget
$image_loc is the directory in which the image is stored
$new_image is the file name of the image

This allows me to:
Swap text for text
Image for image
Text for image
However it fails when I try and swap an image for text. What do I have to do to achieve this?

Replies are listed 'Best First'.
Re: Swapping text and pictures in Labels
by zentara (Cardinal) on Oct 15, 2007 at 12:37 UTC
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::PNG; use MIME::Base64; my $bunny = '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='; my $mw = MainWindow->new(); my $image = $mw->Photo(-data => $bunny); my $label = $mw->Label(-image => $image)->pack(-expand => 1, -fill => +'both'); $mw->Button(-text => 'Image', -command => \&image)->pack; $mw->Button(-text => 'Text', -command => \&text)->pack; $mw->Button(-text => 'Quit', -command => [destroy => $mw])->pack; MainLoop; sub image{ $label->configure(-image => $image); $mw->update; } sub text{ $label->configure(-image => '', -text=>'hoohaa'); $mw->update; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thanks - that is just how I wanted it to behave!