[Tkinter-discuss] [Tutor] displaying an image

Michael Lange klappnase at web.de
Fri Oct 5 21:20:17 CEST 2012


Hi Matthew,

On Fri, 5 Oct 2012 19:10:35 +0100
Matthew Ngaha <chigga101 at gmail.com> wrote:

> with no solution to my problem after 2 days of pulling my hair out:( i
> realize that some libraries are best for the Python version they
> support the most. Sadly most tkinter users use version 2 unlike me, so
> im pretty much on my own as the solutions don't apply to me. Can
> someone please recommend me a toolkit that is fully supported on
> Python 3 and has a similar or easier learning curve to tkinter? thanks
> for all the help and effort guys.

maybe what confused you were some of the more "advanced" suggestions
about using the Python Imaging Library (PIL) or installing a second
Python version or discussing the handling of namespaces.

In fact, as far as I see, there is not too much difference in the
Tkinter usage between Python2.7 and Python3, so for the most part the
old Python2-based Tkinter tutorials should still serve you well. The most
evident difference is that since Python3 the tkinter module spells in
lower case.

Coming back to the problem from your original post:

> im trying to display an image. Ive tried different code from several
> tutorials but they seem outdated and nothing works. here's 1 line that
> makes me give up.
> 
> my_image = Image.open("imagepath.jpg")
> 
> this line returns an error of Image doesnt have attribute open, yet
> this is what my tutorial has shown me. can anyone please help?

This line seems in fact to be taken from some tutorial describing the
usage of PIL. In plain Tkinter the simplest program displaying an image
might look like (Python3 version):


from tkinter import *
root = Tk()
img = PhotoImage(file='imagepath.gif')
l = Label(root, image=img)
l.pack()
root.mainloop()

The only difference to the "Python2-version" is the lower-case "Tkinter"
in the first line.
You see that in Tkinter you usually do not have to use the Image class at
all (although it exists) but create the image directly with the PhotoImage
class.
One (more or less serious) restriction of Tkinter's standard PhotoImage
class is however that you can only use GIF images, other formats like
JPEG or PNG are not supported yet. So unfortunately you will either have
to use an external tool to convert your jpegs into gif first or install a
third-party extension like PIL or TkImg* (the latter is easier to use if
you only need support for more file formats, because no commands are
changed or added, but just support for a lot of image file formats is
added silently in the background; I am not sure however if there is a
precompiled binary for windows systems is easily available) if you really
need these formats.

Did this help any?

Best regards

Michael

* http://sourceforge.net/projects/tkimg/


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

[War] is instinctive.  But the instinct can be fought.  We're human
beings with the blood of a million savage years on our hands!  But we
can stop it.  We can admit that we're killers ... but we're not going
to kill today.  That's all it takes!  Knowing that we're not going to
kill today!
		-- Kirk, "A Taste of Armageddon", stardate 3193.0


More information about the Tkinter-discuss mailing list