self.canvas = canv
def fillContent(self, canv): # override me below
for i in range(10):
canv.create_text(150, 50+(i*100), text='spam'+str(i), fill='beige')
def onDoubleClick(self, event): # override me below
print(event.x, event.y)
print(self.canvas.canvasx(event.x), self.canvas.canvasy(event.y))
if name == 'main': ScrolledCanvas().mainloop()
This script makes the window in Figure 9-24. It is similar to prior scroll examples, but
scrolled canvases introduce two new kinks in the scrolling model:
Scrollable versus viewable sizes
You can specify the size of the displayed view window, but you must specify the
size of the scrollable canvas at large. The size of the view window is what is dis-
played, and it can be changed by the user by resizing. The size of the scrollable
canvas will generally be larger—it includes the entire content, of which only part
is displayed in the view window. Scrolling moves the view window over the scrol-
lable size canvas.
Viewable to absolute coordinate mapping
In addition, you may need to map between event view area coordinates and overall
canvas coordinates if the canvas is larger than its view area. In a scrolling scenario,
the canvas will almost always be larger than the part displayed, so mapping is often
needed when canvases are scrolled. In some applications, this mapping is not re-
quired, because widgets embedded in the canvas respond to users directly (e.g.,
buttons in the PyPhoto example in Chapter 11). If the user interacts with the canvas
directly, though (e.g., in a drawing program), mapping from view coordinates to
scrollable size coordinates may be necessary.
Figure 9-24. scrolledcanvas live
Canvas | 555