Problem displaying images with TkInter

David esuvs at yahoo.co.uk
Thu Mar 4 08:09:45 EST 2004


Hi,
        I'm new to python so I'm probably making a fairly simple mistake, but
look at the example below. It simply displays a rectangle and an image.

-------------------------------------------------
from Tkinter import *

root = Tk()

c = Canvas(root, width=1024, height = 1024)
c.pack()

c.create_rectangle(10,10,50,50)
mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
c.create_image(50, 50, anchor=NW, image=mudPhoto)

root.mainloop()
-----------------------------------------------

Now I try to modify it so that the image and rectangle only appear when I
click the mouse on the canvas. I use the following code:

-----------------------------------------------
from Tkinter import *

root = Tk()

def OnClick(event):
    print "click!!!"
    c.create_rectangle(10,10,50,50)
    mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
    c.create_image(50, 50, anchor=NW, image=mudPhoto)

c = Canvas(root, width=1024, height = 1024)
c.pack()
c.bind("<Button-1>",OnClick)
root.mainloop()
----------------------------------------------

However when I click the mouse it prints 'click!!!' and draws the rectangle
as expected. However it doesn't draw the image! Does anyone have any idea
why this would be?

Thanks for any help,

David



More information about the Python-list mailing list