Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


displaying image using canvas

displaying image using canvas

displaying image using canvas

   How can i display a *.pgm image i called, using canvas function. I'm new to Tcl/Tk.

RE: displaying image using canvas

Displaying an image is a 2-step process:
  1. Create an image object in memory
  2. Display the image object in the canvas (or other widget)
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:

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:

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, www.avia-training.com
  866-TCL-HELP (866-825-4357) US Toll free
  415-643-8692 Voice
  415-643-8697 Fax

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close