How can I display an image in Python 3 using tkinter/ttk?
Question: The nub of the matter is, what am I doing wrong in the following code snippet?

    
    
        from tkinter import *
        from tkinter.ttk import *
    
        root = Tk()
    
        myButton = Button(root)
        myImage = PhotoImage(myButton, file='myPicture.gif')
        myButton.image = myImage
        myButton.configure(image=myImage)
    
        root.mainloop()
    

The error message I get from idle3 is as follows:

    
    
        >>> 
        Traceback (most recent call last):
          File "/home/bob/Documents/Python/tkImageTest.py", line 9, in <module>
            myButton.configure(image=myImage)
          File "/usr/lib/python3.2/tkinter/__init__.py", line 1196, in configure
            return self._configure('configure', cnf, kw)
          File "/usr/lib/python3.2/tkinter/__init__.py", line 1187, in _configure
            self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
        TypeError: __str__ returned non-string (type Button)
        >>> 
    

This error message has me stumped, I simply don't understand what it is trying
to say. Any ideas?

I would also appreciate suggestions for changes...


Answer: The error seems to point to the `myButton` argument passed to `PhotoImage`. As
you noted in your comment, `PhotoImage` was treating the widget object as a
string (there are several options of type string; see a list of PhotoImage
options
[here](http://www.pythonware.com/library/tkinter/introduction/x6716-options.htm)).

Your code will work if you implement that line without referencing the
`myButton` object:

    
    
    myImage = PhotoImage(file='myPicture.gif')
    

I'm not certain you need to alter the `PhotoImage` constructor. Look at the
`PhotoImage` docs to determine the valid options (i.e. resource names) for
that class. Quoting the help file:

> Help on class PhotoImage in module tkinter:
>
> class PhotoImage(Image)
>  
>  
>     | Widget which can display colored images in GIF, PPM/PGM format.
>     |    
>     |  Method resolution order:  
>     |      PhotoImage  
>     |      Image  
>     |      builtins.object  
>     |    
>     |  Methods defined here:
>     |    
>     |  __getitem__(self, key)  
>     |      # XXX config  
>     |    
>     |  __init__(self, name=None, cnf={}, master=None, **kw)  
>     |      Create an image with NAME.
>     |
>     |      Valid resource names: data, format, file, gamma, height, palette, 
>     |      width.
>  

* * *

FYI: The easiest way to get to the docs from Python at the command line or
from IDLE:

    
    
    from tkinter import PhotoImage
    help(PhotoImage)
    

And lastly, another useful link about this class is at
<http://tkinter.unpythonic.net/wiki/PhotoImage>.

