maclean opened this issue on Apr 18, 2009 ยท 12 posts
adp001 posted Sat, 18 April 2009 at 4:59 PM
nruddock is right. Only GIFs or XBM-files (a format mostly used on unixoid OS's).
The way out is indeed PIL. It's easy to use if you have found out to handle it.
The following code is untested, but will give an idea:
import Image as PIL
import ImageTk as PILTK
class ImageButton(Button) :
def __init__(self, master, **kw) :
img=kw.pop("image",None)
if img:
self.TKimage=ImageTk.PhotoImage(PIL.open(img))
kw["image"]=self.TKimage
else :
self.TKimage=None
Button.__init__(self, master, **kw)
The class ImageButton can now be used like a standard button. If you give an imagename, the image is opened via PIL (so nearly any format is supported) and a Tk-compatible image is buffered in class ImageButton (this is needed to avoid that the image is removed via Pythons garbage-collection). The filename is than replaced with a pointer to the image, so the original init-call is done properly.