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!

Adding graphics to buttons in TK

Status
Not open for further replies.

jpasquini

Programmer
Apr 20, 2006
44
US
Help Forum,

I have been scouring the oceans and deserts of the Internet for some simple, easy code for putting a graphic on a button in Perl Tk. Like that embedded image file you might use in Access or VB.

Now dont' talk to me about Widget, because that two page multi conglomerate code snippet of four images with radials and checkbuttons reminds me of a DC10 cockpit manual without my glasses. Plus it appears to use PM files that my version of Perl doesn't have.

Can it be done easily, along the lines of adding an attribute, "-image => c:\images\stop.bmp"??? If anyone knows the short answer, I'll be proud to put it to use.....thanks

jpasquini
 
-image needs to be a Tk::photo object.

Code:
my $icon = new Tk::Photo (-file => "C:\\images\\stop.bmp", -width => 32, -height => 32, -format => 'bmp');

my $button = $top->Button (
  -image => $icon,
)->pack;

The width, height, and format attributes aren't required (it usually tries to guess so it's good to include them if you can). Certain image formats (like PNG) will need additional Tk modules (Tk::pNG respectively) but you'll find that out if you get errors with your Tk::photo call.
 
I should also note that Tk::photo objects can be reused, ie you can use $icon on multiple widgets.

And while we're on the topic of little graphic icons, here's a fun one to change the Tk window's icon from the normal "Tk" logo:

Code:
# where $icon is a Tk::Photo
$top->Icon (-image => $icon);
 
Kirsle,

Much thanks! I will try this soon- jpasquini
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top