Creating an image

Before you can display a graphic in Tk, you must first create a Tk image. To accomplish this, Tk provides the image command. This command allows you to create, delete, and query the keywords for any of the supported image types. The built-in image types are bitmap (two color graphics) and photo. The photo types as shipped in the base distribution are PPM/PGM and GIF.

Based on the keyword used the image command takes several syntactic forms. The syntax and keywords are as follows:

	image keyword path

Keyword

Interpretation

create

Creates an image and a command with the same name.

delete

Deletes the named image.

height

Returns a decimal string containing the height of the image in pixels.

inuse

Returns a Boolean value indicating if the named image provided is currently in use.

names

Returns a list containing the names of all created images.

type

Returns the type of image as referenced by name.

types

Returns a lost containing all valid image types.

width

Returns a decimal string containing the width of the image in pixels.

Getting ready

To complete the following example you will need to have a GIF file to select. Please locate the file tcllogo.gif in your Tcl installation in the demos/Tk8.5/images directory and copy it into your working path.

How to do it…

In the following example we will create an image and display it within a label. Enter the following commands:


1 % image create photo image1 -file tcllogo.gif
.image1

2 % label .l -image image1
.l
3 % pack .l

At this point, your window should look like the following:

How to do it…

How it works…

In the first step, we have created a Tk image of the type photo that references the file tcllogo.gif. Next, we have created a label named .l that contains our image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116