Tk – Images

  • Post author:
  • Post category:Tk
  • Post comments:1 Comment
Tk - Images

The image widget is used to create and manipulate images of Tk. Tk images syntax for creating image is as follows −

image create type name options

In the above syntax type is photo or bitmap and name is the image identifier.

Options

The options available for image create are listed below in the following table −

Sr.No.Syntax & Description
1-file fileNameThe name of the image file name.
2-height numberUsed to set height for widget.
3-width numberSets the width for widget.
4-data stringImage in base 64 encoded string.

A simple example for image widget is shown below −

#!/usr/bin/wish

image create photo imgobj -file "/Users/rajkumar/Desktop/F Drive/pictur/vb/Forests/
   680049.png" -width 400 -height 400 
pack [label .myLabel]
.myLabel configure -image imgobj 

When we run the above program, we will get the following output −

The available function for image are listed below in the following table −

Sr.No.Syntax & Description
1image delete imageNameDeletes the image from memory and related widgets visually.
2image height imageNameReturns the height for image.
3image width imageNameReturns the width for image.
4image type imageNameReturns the type for image.
5image namesReturns the list of images live in memory.

A simple example for using the above image widget commands is shown below −

#!/usr/bin/wish

image create photo imgobj -file "/Users/rajkumar/images/680049.png"
   -width 400 -height 400 
pack [label .myLabel]
.myLabel configure -image imgobj
puts [image height imgobj]
puts [image width imgobj]
puts [image type imgobj]
puts [image names]
image delete imgobj

The image will be deleted visually and from memory once the “image delete imgobj” command executes. In the console, the output will be like the following −

400
400
photo
imgobj ::tk::icons::information ::tk::icons::error ::tk::icons::
warning ::tk::icons::question

Next Topic : Click Here

This Post Has One Comment

Leave a Reply