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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying image using canvas 1

Status
Not open for further replies.

Silentkiller

Technical User
Feb 13, 2003
14
0
0
GB
Hi!
How can i display a *.pgm image i called, using canvas function. I'm new to Tcl/Tk.
Thankx
 
Displaying an image is a 2-step process:[ol][li]Create an image object in memory[/li][li]Display the image object in the canvas (or other widget)[/li][/ol]You create the image object with the image create command, which is where you'd specify the name of the file containing the image data. For example:

Code:
set img [image create photo -file myImg.pgm]

A couple of notes here. The example above assumes that the image file is in the current working directory. If it isn't, you can use a relative or absolutive path for the file name. The second note is that the image create command automatically creates a name for the image object, and that is the return value of the command. You'll need this image name later on, so you'll want to store it in a variable, as shown above.

Now that we've got an image object created, we can display it in our canvas (or other widget). For a canvas, you do so with the canvas's create image operation, and providing it the position and the name of the image object to display. For example:

Code:
canvas .c
pack .c -expand yes -fill both
.c create image 10 10 -anchor nw -image $img

This example tell the canvas, .c, to display the image object I created earlier so that its northwest corner (upper-left corner) is anchored to the position x=10, y=10 on the canvas.

As an aside, Tcl/Tk has built-in support only for GIF and PPM/PGM format full-color images. If you want to display images from other file formats, you'll need to download and install the Img extension, which allows you to display other formats such as JPEG, TIFF, and others. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top