unsettling tkinter bug -- or is it just me?

Bob Lewis bobl at tricity.wsu.edu
Mon Jun 12 21:14:36 EDT 2000


Consider the following code:
--------
from Tkinter import *

root = Tk()
img = PhotoImage(file='bobl.gif')

def func():
    cnvs = Canvas(root, width=img.width(), height=img.height())
    cnvs.create_image(0,0, image=img, anchor=NW)
    cnvs.pack(side=TOP)
    Button(root, text='Quit', command=root.quit).pack()

func()
root.mainloop()
--------
This runs just fine: It creates a root toplevel with "bobl.gif" displayed
in it and a "Quit" button.

Now we make one wafer-thin change: Move the the "img =" inside "func()":
--------
from Tkinter import *

root = Tk()

def func():
    img = PhotoImage(file='bobl.gif')
    cnvs = Canvas(root, width=img.width(), height=img.height())
    cnvs.create_image(0,0, image=img, anchor=NW)
    cnvs.pack(side=TOP)
    Button(root, text='Quit', command=root.quit).pack()

func()
root.mainloop()
--------
And now, while the canvas has the right dimensions, the image does
not appear.  Okay, so "img" went from a global to local scope, but
that shouldn't have affected execution, right?  Have I missed something?

This is on a Red Hat Linux system running versions 1.5.2-13 of both
python and tkinter.

	- Bob Lewis
	  bobl at tricity.wsu.edu



More information about the Python-list mailing list