Construct an icon image in Tcl

Here's a script to build a plain Tk photo image that can be used e.g. as an icon for buttons or labels. It is painted with <image> put rectangles. To convert this into a file, you can serialize it to a base64-encoded string using the approach in Serializing a photo.

The following example constructs a trash can icon image:

package require Tk

set img [image create photo -width 16 -height 16]
set black #000000

# handle (small nub on top):
$img put $black -to 6 0 10 2
# lid (wider bar):
$img put $black -to 2 2 14 4
# body outline: left wall, right wall, bottom:
$img put $black -to 3 4 5 15
$img put $black -to 11 4 13 15
$img put $black -to 3 13 13 15
# three vertical ridge lines inside the body, for a "trash can" read at small size:
$img put $black -to 6 6 7 12
$img put $black -to 8 6 9 12
$img put $black -to 10 6 11 12
your script

Here is how it looks like in ASCII art:

......XXXX......
......XXXX......
..XXXXXXXXXXXX..
..XXXXXXXXXXXX..
...XX......XX...
...XX......XX...
...XX.X.X.XXX...
...XX.X.X.XXX...
...XX.X.X.XXX...
...XX.X.X.XXX...
...XX.X.X.XXX...
...XX.X.X.XXX...
...XX......XX...
...XXXXXXXXXX...
...XXXXXXXXXX...
................

TWu - 2026-09-04 11:06:03

Clever trick! You do not need to declare a variable for "black" as it is a known color-name (see also Tk_GetColor C-function).
With use of expr it is able to scale like SVG (only the lines will be thinner), or the image itself may be scaled. Thanks!