def viewer(imgdir, kind=Toplevel, cols=None): """ измененная версия, выполняет размещение с использованием кнопок фиксированного размера """ win = kind() win.title('Viewer: ' + imgdir) thumbs = makeThumbs(imgdir) if not cols: cols = int(math.ceil(math.sqrt( len(thumbs)))) # фиксированное или N x N savephotos = [] while thumbs: thumbsrow, thumbs = thumbs[:cols], thumbs[cols:] row = Frame(win) row.pack(fill=BOTH) for (imgfile, imgobj) in thumbsrow: size = max(imgobj.size) # ширина, высота photo = PhotoImage(imgobj) link = Button(row, image=photo) handler = lambda savefile=imgfile: ViewOne(imgdir, savefile) link.config(command=handler, width=size, height=size) link.pack(side=LEFT, expand=YES) savephotos.append(photo) Button(win, text='Quit', command=win.quit, bg='beige').pack(fill=X) return win, savephotos
def viewer(imgdir, kind=Toplevel, numcols=None, height=300, width=300): """ use fixed-size buttons, scrollable canvas; sets scrollable (full) size, and places thumbs at absolute x,y coordinates in canvas; caveat: assumes all thumbs are same size """ win = kind() win.title('Simple viewer: ' + imgdir) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = Canvas(win, borderwidth=0) vbar = Scrollbar(win) hbar = Scrollbar(win, orient='horizontal') vbar.pack(side=RIGHT, fill=Y) # pack canvas after bars 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
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
def viewer(imgdir, kind=Toplevel, cols=None): """ измененная версия, размещает миниатюры по сетке """ win = kind() win.title('Viewer: ' + imgdir) thumbs = makeThumbs(imgdir) if not cols: cols = int(math.ceil(math.sqrt( len(thumbs)))) # фиксированное или 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
def viewer(imgdir, kind=Toplevel, cols=None): """ custom version that lays out with fixed-size buttons """ 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 savephotos = [] while thumbs: thumbsrow, thumbs = thumbs[:cols], thumbs[cols:] row = Frame(win) row.pack(fill=BOTH) for (imgfile, imgobj) in thumbsrow: size = max(imgobj.size) # width, height photo = PhotoImage(imgobj) link = Button(row, image=photo) handler = lambda savefile=imgfile: ViewOne(imgdir, savefile) link.config(command=handler, width=size, height=size) link.pack(side=LEFT, expand=YES) savephotos.append(photo) Button(win, text='Quit', command=win.quit, bg='beige').pack(fill=X) return win, savephotos
def viewer(imgdir, kind=Toplevel, numcols=None, height=300, width=300): """ использует кнопки фиксированного размера и холст с возможностью прокрутки; определяет размер области прокрутки (всего холста) и располагает миниатюры по абсолютным координатам x,y холста; предупреждение: предполагается, что все миниатюры имеют одинаковые размеры :param imgdir: :param kind: :param numcols: :param height: :param width: :return: """ win = kind() win.title('Simple viewer: ' + imgdir) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = Canvas(win, borderwidth=0) vbar = Scrollbar(win) hbar = Scrollbar(win, orient='horizontal') vbar.pack(side=RIGHT, fill=Y) hbar.pack(side=BOTTOM, fill=X) canvas.pack(side=TOP, expand=YES, fill=BOTH) vbar.config(command=canvas.yview) hbar.config(command=canvas.xview) canvas.config(yscrollcommand=vbar.set) canvas.config(xscrollcommand=hbar.set) canvas.config(height=height, width=width) thumbs = makeThumbs(imgdir) numthumbs = len(thumbs) if not numcols: numcols = int(math.ceil(math.sqrt(numthumbs))) numrows = int(math.ceil(numthumbs / numcols)) linksize = max(thumbs[0][1].size) fullsize = (0, 0, # координаты верхнего левого угла (linksize * numcols), (linksize * numrows)) # координаты нижнего правого угла canvas.config(scrollregion=fullsize) 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
def viewThumbs(imgdir, kind=Toplevel, numcols=None, height=400, width=500): """ make main or popup thumbnail buttons window; uses fixed-size buttons, scrollable canvas; sets scrollable (full) size, and places thumbs at abs x,y coordinates in canvas; no longer assumes all thumbs are same size: gets max of all (x,y), some may be smaller; """ win = kind() helptxt = '(press D to open other)' win.title(appname + imgdir + ' ' + helptxt) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = ScrolledCanvas(win) canvas.config(height=height, width=width) # init viewable window 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 / float(numcols))) # thumb=(name, obj), thumb.size=(width, height) linksize = max([max(thumb[1].size) for thumb in thumbs]) print linksize 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 win.bind('<KeyPress-d>', onDirectoryOpen) win.savephotos = savephotos return win
def viewer(imgdir, kind=Toplevel, numcols=None, height=300, width=300): """ use fixed-size buttins, scrollable canvas; sets scrollable (full) size, and places thumbs at abs x,y coordinates in canvas; caveat: assumes all thumbs are same size """ win = kind() win.title('Simple viewer: ' + imgdir) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = Canvas(win, borderwidth=0) vbar = Scrollbar(win) hbar = SCrollbar(win, orient='horizontal') vbar.pack(side=RIGHT, fill=Y) # pack canvas after bars hbar.pack(side=BOTTOM, fill=X) # so cliiped last 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 / float(numcols))) 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) 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
def viewThumbs(imgdir, kind=Toplevel, numcols=None, height=400, width=500): """ 构造一个缩略图窗口,当kind=Tk时是主窗体,默认则是一个非模态窗口 使用固定大小的button在canvas上布局 make main or pop-up thumbnail buttons window; uses fixed-size buttons, scrollable canvas; sets scrollable (full) size, and places thumbs at abs x,y coordinates in canvas; no longer assumes all thumbs are same size: gets max of all (x,y), some may be smaller; """ win = kind() helptxt = '(press D to open other)' win.title(appname + imgdir + ' ' + helptxt) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = ScrolledCanvas(win) #构造并布局一个可滚动的画布 canvas.config(height=height, width=width) # init viewable window 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 #将窗口显示弄为N x N,得到列数 numrows = int(math.ceil(numthumbs / numcols)) # 3.x true div #根据指定的列数确定行数 # max w|h: thumb=(name, obj), thumb.size=(width, height) #这里thumbs列表里的项是thumb=(name, obj) linksize = max(max(thumb[1].size) for thumb in thumbs) #所以取thumb[1]得到图片,再获取所有图片中 trace(linksize) #最大的长或宽,以此来确定所有按钮的大小 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) def handler(savefile=imgfile): #按钮的回调,lambda也可以(需要用默认参数 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 win.bind('<KeyPress-d>', onDirectoryOpen) #绑定按键d打开文件夹事件 win.savephotos = savephotos #保持对象引用 return win
def viewer(imgdir, kind=Toplevel, numcols=None, height=300, width=300): win = kind() win.title('Simple viewer: ' + imgdir) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = Canvas(win, borderwidth=0) vbar = Scrollbar(win) hbar = Scrollbar(win, orient='horizontal') vbar.pack(side=RIGHT, fill=Y) hbar.pack(side=BOTTOM, fill=X) canvas.pack(side=TOP, fill=BOTH, expand=YES) vbar.config(command=canvas.yview) hbar.config(command=canvas.xview) canvas.config(yscrollcommand=vbar.set) canvas.config(xscrollcommand=hbar.set) canvas.config(height=height, width=width) thumbs = makeThumbs(imgdir) numthumbs = len(thumbs) if not numcols: numcols = int(math.ceil(math.sqrt(numthumbs))) numrows = int(math.ceil(numthumbs / numcols)) linksize = max(thumbs[0][1].size) fullsize = (0, 0, (linksize * numcols), (linksize * numrows)) canvas.config(scrollregion=fullsize) 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
def viewThumbs(imgdir, kind=Toplevel, numcols=None, height=400, width=500): """ make main or pop-up thumbnail buttons window; uses fixed-size buttons, scrollable canvas; sets scrollable (full) size, and places thumbs at abs x,y coordinates in canvas; no longer assumes all thumbs are same size: gets max of all (x,y), some may be smaller; """ win = kind() helptxt = '(press D to open other)' win.title(appname + imgdir + ' ' + helptxt) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = ScrolledCanvas(win) canvas.config(height=height, width=width) # init viewable window 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 # max w|h: thumb=(name, obj), thumb.size=(width, height) linksize = max(max(thumb[1].size) for thumb in thumbs) trace(linksize) 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) def handler(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 win.bind('<KeyPress-d>', onDirectoryOpen) win.savephotos = savephotos return win
def viewer(imgdir, kind=Toplevel, numcols=None, height=300, width=300): win = kind() win.title('Simple viewer:' + imgdir) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = Canvas(win, borderwidth=0) vbar = Scrollbar(win) hbar = Scrollbar(win, orient='horizontal') vbar.pack(side=RIGHT, fill=Y) hbar.pack(side=BOTTOM, fill=X) canvas.pack(side=TOP, fill=BOTH, expand=YES) vbar.config(command=canvas.yview) hbar.config(command=canvas.xview) canvas.config(yscrollcommand=vbar.set, xscrollcommand=hbar.set) canvas.config(height=height, width=width) thumbs = makeThumbs(imgdir) numthumbs = len(thumbs) if not numcols: numcols = int(math.ceil(math.sqrt(numthumbs))) numrows = int(math.ceil(numthumbs/numcols)) linksize = max(thumbs[0][1].size) fullsize = (0, 0, (linksize * numcols), (linksize * numrows)) canvas.config(scrollregion=fullsize) 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() canvas.create_window(colpos, rowpos, anchor=NW, window=link, width=linksize, height=linksize) colpos += linksize savephotos.append(photo) rowpos += linksize return win, savephotos
def viewer(imgdir, kind=Toplevel, cols=None): win = kind() win.title('Viewer: ' + imgdir) thumbs = makeThumbs(imgdir) if not cols: cols = math.ceil(math.sqrt(len(thumbs))) savephotos = [] while thumbs: thumbsrow, thumbs = thumbs[:cols], thumbs[cols:] row = Frame(win) row.pack(fill=BOTH) for (imgfile, imgobj) in thumbsrow: size = max(imgobj.size) photo = PhotoImage(imgobj) handler = lambda savefile=imgfile: ViewOne(imgdir,savefile) link = Button(row, image=photo) link.config(command=handler, width=size, height=size) link.pack(side=LEFT, expand=YES) savephotos.append(photo) Button(win, text='Quit', command=win.quit()).pack(side=TOP, fill=X) return win, savephotos
def viewer(imgdir, kind=Toplevel, cols=None): win = kind() win.title("Viewer: " + imgdir) thumbs = makeThumbs(imgdir) if not cols: cols = math.ceil(math.sqrt(len(thumbs))) savephotos = [] while thumbs: thumbsrow, thumbs = thumbs[:cols], thumbs[cols:] row = Frame(win) row.pack(fill=BOTH) for (imgfile, imgobj) in thumbsrow: size = max(imgobj.size) photo = PhotoImage(imgobj) handler = lambda savefile=imgfile: ViewOne(imgdir, savefile) link = Button(row, image=photo) link.config(command=handler, width=size, height=size) link.pack(side=LEFT, expand=YES) savephotos.append(photo) Button(win, text="Quit", command=win.quit()).pack(side=TOP, fill=X) return win, savephotos
def viewThumbs(imgdir, kind=Toplevel, numcols=None, height=400, width=500): win = kind() helptxt = '(press D to open other)' win.title(appname + imgdir + ' ' + helptxt) quit = Button(win, text='Quit', command=win.quit, bg='beige') quit.pack(side=BOTTOM, fill=X) canvas = ScrolledCanvas(win) canvas.config(height=height, width=width) thumbs = makeThumbs(imgdir) numthumbs = len(thumbs) if not numcols: numcols = int(math.ceil(math.sqrt(numthumbs))) numrows = int(math.ceil(numthumbs / numcols)) linksize = max(max(thumb[1].size) for thumb in thumbs) trace(linksize) fullsize = (0, 0, linksize * numcols, linksize * numrows) canvas.config(scrollregion=fullsize) 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) def handler(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 win.bind('<KeyPress-d>', onDirectoryOpen) win.savephotos = savephotos return win