Tkinter question: image on canvas

Hans Nowak ivnowa at hvision.nl
Sat Dec 4 16:19:29 EST 1999


Howdy y'all,

I have been trying to do something fairly straightforward in Tkinter, 
viz. putting an image on a canvas. This code, however, doesn't work:

-----------begin code---------

from Tkinter import *

class MemoryFrame (Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.createWidgets()

    def createWidgets(self):
        self.canvas = Canvas(self, width=400, height=400,
        background='white')
        self.canvas.pack(side=TOP)

        self.createImage()

    def createImage(self):
        img = PhotoImage(file="c:/001.gif")
        print img
        id = self.canvas.create_image(40, 40, image=img, anchor=NW)
        print id


if __name__ == "__main__":

    mf = MemoryFrame()
    mf.pack()
    mf.mainloop()

----------end code---------------------

It doesn't raise any errors, but doesn't show the image either! I've 
checked if it's a valid image, I've checked if 'img' and 'id' print 
sensible values... everything seems normal except for what I see on 
the screen. I've tried to put other things (rectangles and such) on 
the canvas... works fine.

The strange thing is, if I simply make a script which roughly does 
the following:

root = Tk()
canvas = Canvas(...)
canvas.create_image(...)
root.mainloop()

then it does work. >_<  Even stranger, the first approach worked in a 
different program... so I took that file, snipped some seemingly 
unrelated code, and ta-da! It didn't work anymore. I'm clueless. =(

What am I missing? 

--Hans Nowak (zephyrfalcon at hvision.nl)
Homepage: http://fly.to/zephyrfalcon
You call me a masterless man. You are wrong. I am my own master.




More information about the Python-list mailing list