Displaying an image in a Tk widget

Displaying an image in a Tk widget

James Wettenhall & Philippe Grosjean bio photo By James Wettenhall & Philippe Grosjean

The following code displays a GIF image in a Tk window (actually in a Tk label widget within a Tk window).


# Tk supports natively gif images, like this one:
imgfile <- system.file("gui/SciViews.gif", package = "tcltk2")
image1 <- tclVar()
tkimage.create("photo", image1, file = imgfile)

# Create a Tk window with a label displaying this image
win1 <- tktoplevel()
win1$env$label <- tk2label(win1, image = image1)

image in a Tk label

Tk supports natively PNG, GIF, PPM and PGM images. For other formats, you can install the Tk Img package and use tclRequire("Img") in R to make it available to R tcltk. From this moment on, you have also access to TIFF, JPEG, BMP, XBM, …, images.