Tkinter displaying an image in a button

David R. Smith drs at alex2.labs.agilent.com
Wed Nov 21 18:02:45 EST 2001


The first code snippet below successfully displays an image, whereas the
second just shows gray where the image should be.  What's wrong with the
class version, and what do I have to do to get it to work? 
(I am using Python 2.0.1 on Debian.)

    adTHANKSvance
    David Smith



from Tkinter import *
import Image, ImageTk
root = Tk()
img = Image.open('SonicCruiser.jpg')
phi = ImageTk.PhotoImage(img)
button = Button(root, image=phi)
button.pack()
root.mainloop()



from Tkinter import *
import Image, ImageTk
class App:
    def __init__(self, master):
        img = Image.open('SonicCruiser.jpg')
        phi = ImageTk.PhotoImage(img)
        button = Button( master, image=phi )
        button.pack()
root = Tk()
app = App(root)
root.mainloop()



More information about the Python-list mailing list