image create

image create type ?name? ?option value ...?

Creates a new image and returns its name. type specifies the type of the image, which must be one of the types currently defined (e.g., bitmap). name specifies the name for the image; if it is omitted then Tk picks a name of the form imagex, where x is an integer. There may be any number of option-value pairs, which provide configuration options for the new image. The legal set of options is defined separately for each image type; see below for details on the options for built-in image types. If an image already exists by the given name then it is replaced with the new image and any instances of that image will redisplay with the new contents. It is important to note that the image command will silently overwrite any procedure that may currently be defined by the given name, so choose the name wisely. It is recommended to use a separate namespace for image names (e.g., ::img::logo, ::img::large).

MGS [2003/09/15] - Note that image create will overwrite existing images of the form imagex, where x is an integer:

 % image create photo image1
 image1
 % image create photo
 image1

It appears that Tk uses an internal counter for image names, and does not check if imagex already exists.

In fact, Tcl in many cases does not prevent one from creating objects that are named the same as an existing one. The difference here is that Tk is automatically generating a name that happens to match. That is the unfortunate feature.

DGP: This bug (Tk Bug 808039) is corrected for Tk 8.4.5.

DKF: In the meantime, don't create images explicitly named like image%d and you'll be OK.

MGS: The reason I discovered this bug, was that I am writing a utility to dump the complete state of an interp to a file. Sourcing that dumped file into a new interp recreated all imagex images as named images, and then all image create commands then started overwriting ... I ended up actually overloading the image command and making sure the internal image counter was in sync with given image names.

See also: