Loading Images#
# with tkinter 8.6:
self.buttonPhoto = PhotoImage(file='../images/butImage.png')
buttonPhotoTrans = PhotoImage(file='../images/butImageTrans.png')
# with tkinter 8.5:
from PIL import Image, ImageTk
im1 = Image.open('../images/butImage.png')
^^^
im2 = Image.open('../images/butImageTrans.png')
self.buttonPhoto = ImageTk.PhotoImage(im1)
^^^
buttonPhotoTrans = ImageTk.PhotoImage(im2)
The images butImage.png and butImageTrans.png are referenced to the images file. The image is loaded into PhotoImage, where a reference is required which will be used within the widget's property option "image".
Warning
If images are used within a class there is always the problem that garbage collection will dispose of the image and it will not show unless special precautions are taken.
When the image is a local variable, reload the image directly after referencing it with the widget, alternatively in class ensure that the image variable is prefixed by self, (compare how the two images self.buttonPhoto and buttonPhotoTrans are treated).