You are here


8 September, 2015 - 10:43

Exercise 19.4. For this exercise, you will write an image viewer. Here is a simple example:

g = Gui()canvas = = PhotoImage(file='danger.gif')canvas.image([0,0], image=photo)g.mainloop()

PhotoImagereads a file and returns aPhotoImageobject that Tkinter can display. Canvas.imageputs the image on the canvas, centered on the given coordinates. You can also put images on labels, buttons, and some other widgets:

PhotoImage can only handle a few image formats, like GIF and PPM, but we can use the Python Imaging Library (PIL) to read other files.

The name of the PIL module isImage, but Tkinter defines an object with the same name. To avoid the conflict, you can useimport...aslike this:

import Image as PILimport ImageTk

The first line imports Imageand gives it the local name PIL. The second line imports ImageTk, which can translate a PIL image into a Tkinter PhotoImage. Here’s an example:

image ='allen.png')photo2 = ImageTk.PhotoImage(image)


Download, danger.gifand allen.pngfrom http: // code. Run You might have to install PILand ImageTk. They are probably in your software repository, but if not you can get them from http: //pythonware. com/ products/ pil .


In image_demo.pychange the name of the second PhotoImage from photo2to photoand run the program again. You should see the second PhotoImage but not the first.

The problem is that when you reassign photoit overwrites the reference to the first PhotoImage, which then disappears. The same thing happens if you assign a PhotoImage to a local variable; it disappears when the function ends.

To avoid this problem, you have to store a reference to each PhotoImage you want to keep. You can use a global variable, or store PhotoImages in a data structure or as an attribute of an object.

This behavior can be frustrating, which is why I am warning you (and why the example image says “Danger!”).


Starting with this example, write a program that takes the name of a directory and loops through all the files, displaying any files that PIL recognizes as images. You can use a trystatement to catch the files PIL doesn’t recognize.

When the user clicks on the image, the program should display the next one.


PIL provides a variety of methods for manipulating images. You can read about them at http: // pythonware. com/ library/ pil/ handbook. As a challenge, choose a few of these methods and provide a GUI for applying them to images.


Solution: http: // thinkpython. com/ code/ ImageBrowser. py .

Exercise 19.5. A vector graphics editor is a program that allows users to draw and edit shapes on the screen and generate output files in vector graphics formats like Postscript and SVG.

Write a simple vector graphics editor using Tkinter. At a minimum, it should allow users to draw lines, circles and rectangles, and it should use Canvas.dumpto generate a Postscript description of the contents of the Canvas.

As a challenge, you could allow users to select and resize items on the Canvas.

Exercise 19.6. Use Tkinter to write a basic web browser. It should have a Text widget where the user can enter a URL and a Canvas to display the contents of the page.

You can use theurllibmodule to download files (see Exercise 14.6 in Exercises) and theHTMLParsermodule to parse the HTML tags (seehttp: // docs. python. org/ 2/ library/ htmlparser. html ).

At a minimum your browser should handle plain text and hyperlinks. As a challenge you could handle background colors, text formatting tags and images.