Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image on a user button (groan....)

Status
Not open for further replies.

jpasquini

Programmer
Apr 20, 2006
44
US
Hi all,

Has anyone seen a quick and dirty example of adding a graphical image to a button in Perl TK? All I have is this monstrosity from the 'widget' module, which is like learning to drive the space shuttle to get to the mailbox.........

# icon.pl

use vars qw/$TOP/;

sub icon {

# Create a top-level window that displays a bunch of iconic buttons.

my($demo) = @_;
$TOP = $MW->WidgetDemo(
-name => $demo,
-text => ['This window shows three ways simultaneously of using bitmaps or images in radiobuttons and checkbuttons. On the left are two radiobuttons, each of which displays a bitmap and an indicator. In the middle is a checkbutton that displays a different image depending on whether it is selected or not. On the right is a checkbutton that displays a single bitmap but changes its background color to indicate whether or not it is selected.', qw/-wraplength 5i/],
-title => 'Iconic Button Demonstration',
-iconname => 'icon',
);

$TOP->Bitmap('flagup',
-file => Tk->findINC('demos/images/flagup'),
-maskfile => Tk->findINC('demos/images/flagup'),
);
$TOP->Bitmap('flagdown',
-file => Tk->findINC('demos/images/flagdown'),
-maskfile => Tk->findINC('demos/images/flagdown'),
);

my $frame = $TOP->Frame(qw/-borderwidth 10/);
$frame->pack(qw/-side top/);

my(@pl) = qw/-side left -expand yes -padx 5m/;
my $frame_left = $frame->Frame;
$frame_left->pack(@pl);

my $frame_b1 = $frame->Checkbutton(
-image => 'flagdown',
-selectimage => 'flagup',
-indicatoron => 0,
);
$frame_b1->pack(@pl);
$frame_b1->configure(-selectcolor => $frame_b1->cget(-background));
my $frame_b2 = $frame->Checkbutton(
-bitmap => '@' . Tk->findINC('demos/images/letters'),
-indicatoron => 0,
-selectcolor => 'SeaGreen1',
);
$frame_b2->pack(@pl);

my $letters = '';
@pl = qw/-side top -expand yes/;
my $frame_left_b3 = $frame_left->Radiobutton(
-bitmap => '@' . Tk->findINC('demos/images/letters'),
-variable => \$letters,
-value => 'full',
);
$frame_left_b3->pack(@pl);
my $frame_left_b4 = $frame_left->Radiobutton(
-bitmap => '@' . Tk->findINC('demos/images/noletters'),
-variable => \$letters,
-value => 'empty',
);
$frame_left_b4->pack(@pl);

} # end icon

1;


I envisioned something like this, but no dice. The button then shrinks to the size of a dot, and nothing is readable (setting dimesions on the button didn't work either)........


my $graphics = '/opt/perl/lib/site_perl/5.8.0/PA-RISC1.1-thread-multi/Tk/demos/images'; ## Directory with default images


## Add graphic (sample)
$mw-> Bitmap('flagup',
-file => Tk-> findINC ("${graphics}/flagup"),
-maskfile=> Tk-> findINC ("${graphics}/flagup"),
);

my $btn_release_mail= $w_Commands
-> Button( #-text => 'Exit',
-command => \&_release_mail,
-font => "{Arial} 11 {bold}",
-image => 'flagup',
)
-> pack( -side => 'left', -pady => '10', -padx => '15' );



 
Forum,

Here's the answer to the above question:

## Add graphic (sample)
my $pic_mailbox = $mw-> Photo ( -file => "${graphics}/flagup");

my $btn_release_cancel= $w_Commands
-> Button( #-text => 'Exit',
-command => \&_release_exit,
-font => "{Arial} 11 {bold}",
-image => $pic_mailbox,
)
-> pack( -side => 'left', -pady => '10', -padx => '15' );




Also, let me highly recommend "Learning Perl/TK" by Nancy Walsh (O'Reilly, 1999, with the baby chick on it).
This book is out of print, but time and again I have found straighforward answers in it, and well explained. It is far, far better than the "Mastering Perl/TK" book in the series, which is in my opinion incredibly frustrating (for instance, the index isn't even correct).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top