Scrolling images too: PyPhoto (ahead)
Despite its evolutionary twists, the scrollable thumbnail viewer in Example 9-15 still
has one major limitation remaining: images that are larger than the physical screen are
simply truncated on Windows when popped up. This becomes glaringly obvious when
opening large photos copied from a digital camera like those in Figure 9-25. Moreover,
there is no way to resize images once opened, to open other directories, and so on. It’s
a fairly simplistic demonstration of canvas programming.
In Chapter 11, we’ll learn how to do better when we meet the PyPhoto example pro-
gram. PyPhoto will scroll the full size of images as well. In addition, it has tools for a
variety of resizing effects, and it supports saving images to files and opening other image
directories on the fly. At its core, though, PyPhoto will reuse the techniques of our
simple browser here, as well as the thumbnail generation code we wrote in the prior
chapter; much like our simple text editor earlier in the chapter, the code here is essen-
tially a prototype for the more complete PyPhoto program we’ll put together later in
Chapter 11. Stay tuned for the thrilling conclusion of the PyPhoto story (or flip ahead
now if the suspense is too much to bear).
For the purposes of this chapter, notice how in Example 9-15 the thumbnail viewer’s
actions are associated with embedded button widgets, not with the canvas itself. In
fact, the canvas isn’t much but a display device. To see how to enrich it with events of
its own, let’s move on to the next section.
Using Canvas Events
Like Text and Listbox, there is no notion of a single command callback for Canvas. Instead,
canvas programs generally use other widgets (as we did with Example 9-15’s thumbnail
buttons) or the lower-level bind call to set up handlers for mouse clicks, key presses,
and the like (as we did for Example 9-14’s scrolling canvas). Example 9-16 takes the
latter approach further, showing how to bind additional events for the canvas itself, in
order to implement a few of the more common canvas drawing operations.
Example 9-16. PP4E\Gui\Tour\canvasDraw.py
"""
draw elastic shapes on a canvas on drag, move on right click;
see canvasDraw_tags*.py for extensions with tags and animation
"""
from tkinter import *
trace = False
class CanvasEventsDemo:
def init(self, parent=None):
canvas = Canvas(width=300, height=300, bg='beige')
canvas.pack()
canvas.bind('
canvas.bind('
560 | Chapter 9: A tkinter Tour, Part 2