TKinter PhotoImage Class?? - 2nd round

Robert C. Ramsdell rcriii at ramsdells.net
Tue Oct 16 05:10:11 CEST 2001


A while back Matthew Dixon suggested the following bit of code to display a 
jpeg on a Tkinter Canvas:

from Tkinter import *
import PIL.ImageTk
r=Tk()
i=PIL.ImageTk.PhotoImage(file="/home/rcriii/python/rempyre/tree32.jpg")
c=Canvas(r)
c.pack()
c.create_image(16,16,image=i)
r.mainloop()

This works fine.  However, in trying to adapt it for an application I have 
a problem.  I have built the display in a class, with the Canvas as an 
attribute of the class, and a method to draw the image in the Canvas (code 
at the end of this message).  When I try the method above in the method, 
the Canvas remains empty.  No errror message is given, the image just is 
not displayed.  Does anyone know why this is happening?

import Tkinter
from Tkconstants import *
import PIL.ImageTk

class display:
        def __init__(self):
                self.tk = Tkinter.Tk()
                self.drawspace = Tkinter.Canvas(self.tk, background="white")
                self.drawspace.pack()

        def mainloop(self):
                self.tk.mainloop()

        def draw_image(self, iname):
                self.drawspace.pack()
                i=PIL.ImageTk.PhotoImage(file=iname)
                self.drawspace.create_image(0,0,image=i)


if __name__ == "__main__":
        m=display()
        m.draw_image("/home/rcriii/python/rempyre/tree32.jpg")
        m.mainloop()



More information about the Python-list mailing list