Tkinter not displaying image
Question: I have this code:

    
    
    from tkinter import *
    import urllib
    import urllib.request
    from bs4 import BeautifulSoup
    import Pydeck
    import sys
    from collections import defaultdict
    
    root = Tk()
    name=""
    def buttonclicked():
        name()
        picture()
    
    def name():
    
        all_lists=[] #all lists
        text = inputfield.get()
        Pydeck.loadDatabase('DATABASE PATH')
        cardName = Pydeck.getCardsFromName(text)
        if not cardName == "":
                c = Pydeck.Card(cardName)
        tex.insert(END, c.name)
        level="\nLevel %s" % c.level + " " + c.attribute + " " + c.typestring 
        tex.insert(END, level)
        atk="\nAtk: %s" % c.attack
        tex.insert(END, atk)
        defe="\nDef: %s" % c.defense
        tex.insert(END, defe)
        typestring='\n%s' %c.typestring
        desc='\n%s' %c.description
        seperator='\n--------------------\n'
        tex.insert(END, typestring)
        tex.insert(END, desc)
        tex.insert(END,seperator)
        #--
        tex.see(END)             # Scroll if necessary
        return c.cardID
    
    
    def picture():    
        text = inputfield.get()
        gifdir = "PICTURE FOLDERS PATH"
        Pydeck.loadDatabase('DATABASE PATH')
        cardName = Pydeck.getCardsFromName(text)
        if not cardName == "":
            c=Pydeck.Card(cardName)
        filename='{}.gif' .format(c.cardID)
        img = PhotoImage(file=gifdir+filename)
        can = Canvas(root)
        can.pack(fill=BOTH,side='top')
        can.config(width=img.width(), height=img.height())        
        can.create_image(2, 2, image=img, anchor=NW)
    
    tex=Text(root)
    tex.pack(side='right')
    inputfield = Entry(root)
    inputfield.pack(side='bottom')
    but = Button(root,text="Enter Name", command = buttonclicked) #Calls name function
    but.pack(side='bottom')
    text = inputfield.get()
    
    root.mainloop()
    

Ok, so in short the the program is in Tkinter for Python 3.3. It asks the user
to input a name of a monster, it then searches for the monster in the database
and returns the monster's attributes this is done in the `name` function. I
want it to display a picture of the monster which is where I'm running into
trouble. So the `picture` function takes one of the attributes specifically
the `cardID` attribute and uses that as the name of the gif file. However it
doesn't display the picture and I don't know why. Does anyone know why?


Answer: In order to get a stripped-down version of your code to display a .gif test
image, I had to add a`global img`statement to the`picture()`function so
the`PhotoImage`object that variable name refers to would not be destroyed when
the function returns.

It seems strange that`Canvas.create_image()`doesn't increment the reference
count of the`image`object it's passed...looks like it's a [documented
bug](http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm).

**Update:** For reasons I don't completely understand, adding a`can.img =
img`right after the`can = Canvas(root)`in the`picture()`function accomplishes
the same thing in a better way than creating a global named`img`in my opinion.

Display from working version:

![a tk window showing test image being
displayed](http://i.stack.imgur.com/Jr9Jx.png)

