[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
hbar.pack(side=BOTTOM, fill=X) # so clipped first
canvas.pack(side=TOP, fill=BOTH, expand=YES)

vbar.config(command=canvas.yview) # call on scroll move
hbar.config(command=canvas.xview)
canvas.config(yscrollcommand=vbar.set) # call on canvas move
canvas.config(xscrollcommand=hbar.set)
canvas.config(height=height, width=width) # init viewable area size
# changes if user resizes
thumbs = makeThumbs(imgdir) # [(imgfile, imgobj)]
numthumbs = len(thumbs)
if not numcols:
numcols = int(math.ceil(math.sqrt(numthumbs))) # fixed or N x N
numrows = int(math.ceil(numthumbs / numcols)) # 3.x true div

linksize = max(thumbs[0][1].size) # (width, height)
fullsize = (0, 0, # upper left X,Y
(linksize * numcols), (linksize * numrows) ) # lower right X,Y
canvas.config(scrollregion=fullsize) # scrollable area size

rowpos = 0
savephotos = []
while thumbs:
thumbsrow, thumbs = thumbs[:numcols], thumbs[numcols:]
colpos = 0
for (imgfile, imgobj) in thumbsrow:
photo = PhotoImage(imgobj)
link = Button(canvas, image=photo)
handler = lambda savefile=imgfile: ViewOne(imgdir, savefile)
link.config(command=handler, width=linksize, height=linksize)
link.pack(side=LEFT, expand=YES)
canvas.create_window(colpos, rowpos, anchor=NW,
window=link, width=linksize, height=linksize)
colpos += linksize
savephotos.append(photo)
rowpos += linksize
return win, savephotos

if __name__ == '__main__':
imgdir = 'images' if len(sys.argv) < 2 else sys.argv[1]
main, save = viewer(imgdir, kind=Tk)
main.mainloop()

To see this program in action, make sure you’ve installed the PIL extension described
near the end of Chapter 8 and launch the script from a command line, passing the name
of the image directory to be viewed as a command-line argument:
...\PP4E\Gui\PIL> viewer_thumbs_scrolled.py C:\Users\mark\temp\101MSDCF
As before, clicking on a thumbnail image opens the corresponding image at its full size
in a new pop-up window. Figure 9-25 shows the viewer at work on a large directory
copied from my digital camera; the initial run must create and cache thumbnails, but
later runs start quickly.

558 | Chapter 9: A tkinter Tour, Part 2

Do


wnload from Wow! eBook <www.wowebook.com>

Free download pdf