Skip to main content
Engineering LibreTexts

10.11: Exercises

  • Page ID
    17130
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Exercise \(\PageIndex{1}\)

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

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

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

    g.la(image=photo)
    g.bu(image=photo)
    

    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 is Image, but Tkinter defines an object with the same name. To avoid the conflict, you can use import...as like this:

    import Image as PIL
    import ImageTk
    

    The first line imports Image and 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 = PIL.open('allen.png')
    photo2 = ImageTk.PhotoImage(image)
    g.la(image=photo2)
    
    1. Download image_demo.py, danger.gif and allen.png from http://thinkpython.com/code. Run image_demo.py. You might have to install PIL and ImageTk. They are probably in your software repository, but if not you can get them from http://pythonware.com/products/pil.
    2. 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.

    3. When the user clicks on the image, the program should display the next one.
    4. 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 \(\PageIndex{2}\)

    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.dump to 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 \(\PageIndex{3}\)

    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 the urllib module to download files (see Exercise 14.12.1) and the HTMLParser module to parse the HTML tags (see http://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.


    This page titled 10.11: Exercises is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?