Need Tkinter help - Label with image

Daryl P McDaniel python at mc2research.org
Sat Jan 12 15:35:28 EST 2002


Hello,

I am having a problem with Tkinter that I don't understand.  I have consulted
"Python and Tkinter Programming", by John Grayson, and "An Introduction
to Tkinter", by Fredrik Lundh, without finding an answer.

Basically, I can't get a Label to display an image if both the PhotoImage
and Label calls are contained within the same function.  I can make my
application work, but don't want to ship it until I understand what is
going on.

TASK:
    Display a color image in a label within an application.

PROBLEM:
    When both the PhotoImage and Label function calls are made in the same
    function, the Label is blank, but at the correct size.
    If the PhotoImage and Label function calls are not in the same function,
    the Label displays the image properly.

TEST ENVIRONMENTS:
    Linux, RedHat 7.2; Python 1.5.2; TCL/TK 8.3
    Linux, RedHat 7.2; Python 2.2; TCL/TK 8.3
    Windows 98; Python 2.0; TCL/TK 8.3.2

DESCRIPTION:
    The following 4 Test Programs illustrate the problem.
    Test 1 Good - displays the Label with the image.
    Test 2 BAD  - displays a Label of correct size but the image is blank.
    Test 3 Good - displays the Label with the image.
    Test 4 Good - displays the Label with the image.

    Why doesn't Test 2 work?

---------------------------------------+---------------------------------------
# Test 1                               |  # Test 2
# PhotoImage at file scope             |  # PhotoImage in a function
# Label creation in a function         |  # Label creation in same function
                                       |
from Tkinter import *                  |  from Tkinter import *
                                       |
def mkLab(master, img):                |  def mkLab(master):
        l1 = Label(master, image=img)  |      img = PhotoImage(file="timg.gif")
        l1.pack()                      |      l1  = Label(master, image=img)
        return l1                      |      l1.pack()
                                       |      return l1
                                       |
root = Tk()                            |  root = Tk()
root.title("PI FS Lab Fun")            |  root.title("PI+Lab Fun")
                                       |
pict  = PhotoImage(file="timg.gif")    |
myLab = mkLab(root, pict)              |  myLab = mkLab(root)
                                       |
root.mainloop()                        |  root.mainloop()
=======================================+=======================================
# Test 3                               |  # Test 4
# PhotoImage in a function             |  # PhotoImage in a function
# Label creation at file scope         |  # Label creation in other function
                                       |
from Tkinter import *                  |  from Tkinter import *
                                       |
def loadImage():                       |  def loadImage():
    img = PhotoImage(file="timg.gif")  |      img = PhotoImage(file="timg.gif")
    return img                         |      return img
                                       |
                                       |  def mkLab(master, pict):
                                       |      l1 = Label(master, image=pict)
                                       |      l1.pack()
                                       |      return l1
                                       |
root = Tk()                            |  root = Tk()
root.title("PI Fun Lab FS")            |  root.title("PI Fun Lab Fun")
                                       |
myPic = loadImage()                    |  myPic = loadImage()
l1 = Label(root, image=myPic)          |  myLab = mkLab(root, myPic)
l1.pack()                              |
                                       |
root.mainloop()                        |  root.mainloop()
---------------------------------------+---------------------------------------

Thank you,

Daryl P McDaniel        python at mc2research.org
MC2 Research
Portland, Oregon  USA



More information about the Python-list mailing list