Using Images in the GUI
August 8, 2011 6 Comments
Gladys leaps over to the tape deck, presses levers and switches. Sound of tape reversing. There is a hum and lights flash on and off. A blurred image of a lady in the street comes up on one of the monitors.
In this tutorial we look at using images in a GUI environment. One of the possible attributes which can be assigned to a Label or Button widget is an image. You will surely have seen many buttons with images on them. Every time you see a toolbar in a program, each of the images on it is itself on a button widget. In order to do this tutorial though there are a couple of issues:
- first, you need to have images in the Python4kids directory we set up several tutorials ago. You also need to start Python from within that directory.
- second, unless you install something called the Python Imaging Library (PIL), Python can only handle a few limited types of image file. One of the file types which will work is .gif, which we will be using here. This tutorial will not work with jpeg files.
- third, you need to have the gif files we’re going to use in your python for kids directory.
If you have some gifs that you’d prefer to use, by all means copy them into your python for Kids directory. However, if you can’t, here is an outrageously boring gif (it is just a screencapture from the previous tutorial) for you to download and save:
Download and save this gif image by right-clicking the images in the previous tutorial and selecting “Save Link As” (you may also be able to drag and drop images from the web page into your folder) [* or do it in Python (below)]. Remember to save it to the Python for kids directory that you set up in this tutorial. You don’t need to change the file name. If you have your own image file(s) you’d like to use, then, by all means put them in your Python4kids directory instead but make sure it is a ‘.gif’ file. You can convert images from .jpg to gif using third party programs such as GIMP.
As before we import from Tkinter:
>>> from Tkinter import * >>> labelWidget = Label(None) >>> # Notice we haven't set the text?
Next, we make a PhotoImage object from the file (taking note of the file’s name as we downloaded it)
>>> imgFile = 'p4ktkinter110726b.gif' >>> imageObject = PhotoImage(file = imgFile)
Watch the “file = ” syntax here. We haven’t attached the file to the labelWidget yet. We’ll do that very soon. However, now if we:
You’ll see that we get a really small labelWidget with nothing in it. You might need to resize it to see much of it at all.
So let’s put the image we have into the labelWidget:
Note here that we are using the dictionary associated with labelWidget, you should be able to tell that because of the square brackets and the key which is inside single quotes. At this point the object should automagically appear in the widget – if you haven’t resized the window yet you may need to in order to see the image:
It looks like there is a window inside the window, but there isn’t. It’s just an image (try opening up the gif file and changing it with a paint package if you have one and then redoing the tutorial). Clicking the buttons won’t have any effect.
Display an image on a Button
* Extension: Downloading the file using Python:
>>> url = "https://python4kids.files.wordpress.com/2011/08/p4ktkinter110726b.gif" >>> fileName = url[-21:] >>> fileName 'p4ktkinter110726b.gif' >>> import urllib >>> urllib.urlretrieve(url,fileName) ('p4ktkinter110726b.gif', <httplib.HTTPMessage instance at 0x82695ac>)
This relies only on knowing what the url of the image file is. We’re using the urlretrieve function of the urllib module. It will work for urls which are not images as well. In order to download a file it needs to know the url of the file (ie what you would type into a web browser to surf to the file) and a file name to save the to on your computer.
A word of warning though – sometimes a network is set up so you that this will not work. If you have trouble, better to just save as with your browser.