Mailing List Archive

Tkinter bug ?
File1.py:

from Tkinter import *

photo=None

def create_photo():
global photo
photo=PhotoImage(image="img.gif")



File2.py:

from Tkinter import *
from File1 import *

root = Tk()
create_photo()
b= Button(root, image=photo)
b.pack()
root.mainloop()


when executing File2.py, there is no error, but no image appears in the
button.

now if you chanage File2.py:

from Tkinter import *
import File1

root = Tk()
File1.create_photo()
b= Button(root, image=File1.photo)
b.pack()
root.mainloop()

it work fine. It seems to be a bug, isn't it ?

--
Stéphane Conversy
http://www-ihm.lri.fr/~conversy/
mailto:conversy@lri.fr
Tkinter bug ? [ In reply to ]
Stephane Conversy <conversy@lri.fr> wrote:
> when executing File2.py, there is no error, but no image appears in the
> button.
>
> it work fine. It seems to be a bug, isn't it ?

it's a feature. see:
http://www.python.org/doc/FAQ.html#4.69

</F>
Tkinter bug ? [ In reply to ]
FAQ:

Well, the Tk button widget keeps a reference to the internal
photoimage object, but Tkinter does not. So when the last
Python reference goes away, Tkinter tells Tk to release the
photoimage. But since the image is in use by a widget, Tk
doesn't destroy it. Not completely. It just blanks the image,
making it completely transparent...

ok, but does it have something to do with the fact that in one case
I do a from file1 import * and in the other import file1 ?

I'm a beginner in Python, and don't know exaclty how modules
are implemented...

stef


--
Stéphane Conversy
http://www-ihm.lri.fr/~conversy/
mailto:conversy@lri.fr
Tkinter bug ? [ In reply to ]
Stephane Conversy <conversy@lri.fr> wrote:
> ok, but does it have something to do with the fact that in one case
> I do a from file1 import * and in the other import file1 ?

now that you mention it...

forget my previous answer (for the moment; you
might stumble upon that problem later).

...

if you'd added some "print photo" statements to File2.py
in your first example, you would have noticed that the
variable is None both before and after the call to
create_photo(). if you'd added similar statements to
File1.py, you would have noticed that create_photo
actually did set it to something else.

the reason for this is that all variables in Python contain
references to objects, not the objects themselves. the
assignment statement just changes a variable to refer to
a new value, it doesn't copy the object itself (nor does
this modify the old value).

and import is just a fancy assignment statement; the
from-import form copies object references from the
imported module, not the objects itself.

in other words, after the import, there are TWO "photo"
variables, one in each module. and changing one to refer
to a new object doesn't automatically change the other
one.

> I'm a beginner in Python, and don't know exaclty how modules
> are implemented...

but in that case, you shouldn't be using from-import at all !

for more details, see:
http://www.pythonware.com/people/fredrik/fyi/fyi06.htm

note the fourth item under "Which Way Should I Use?" ;-)

</F>