def viewer(imgdir, kind=Toplevel, cols=None):
"""
custom version that uses gridding
"""
win = kind()
win.title('Viewer: ' + imgdir)
thumbs = makeThumbs(imgdir)
if not cols:
cols = int(math.ceil(math.sqrt(len(thumbs)))) # fixed or N x N
rownum = 0
savephotos = []
while thumbs:
thumbsrow, thumbs = thumbs[:cols], thumbs[cols:]
colnum = 0
for (imgfile, imgobj) in thumbsrow:
photo = PhotoImage(imgobj)
link = Button(win, image=photo)
handler = lambda savefile=imgfile: ViewOne(imgdir, savefile)
link.config(command=handler)
link.grid(row=rownum, column=colnum)
savephotos.append(photo)
colnum += 1
rownum += 1
Button(win, text='Quit', command=win.quit).grid(columnspan=cols, stick=EW)
return win, savephotos
if name == 'main':
imgdir = (len(sys.argv) > 1 and sys.argv[1]) or 'images'
main, save = viewer(imgdir, kind=Tk)
main.mainloop()
Figure 8-47 displays the effect of gridding—our buttons line up in rows and columns
in a more uniform fashion than in Figure 8-45, because they are positioned by both row
and column, not just by rows. As we’ll see in the next chapter, gridding can help any
time our displays are two-dimensional by nature.
Layout options: Fixed-size buttons
Gridding helps—rows and columns align regularly now—but image shape still makes
this less than ideal. We can achieve a layout that is perhaps even more uniform than
gridding by giving each thumbnail button a fixed size. Buttons are sized to their images
(or text) by default, but we can always override this if needed. Example 8-48 does the
trick. It sets the height and width of each button to match the maximum dimension of
the thumbnail icon, so it is neither too thin nor too high. Assuming all thumbnails have
the same maximum dimension (something our thumb-maker ensures), this will achieve
the desired layout.
502 | Chapter 8: A tkinter Tour, Part 1