嘿伙计们我正在使用Tkinter处理图像阅读GUI,试图在画布上制作一个自动调整大小的框架,当新图像添加到框架中时,它的滚动条应该更新。 我在网上找到了一个非常有用的例子,我将其修改为更一般的例子:
import Tkinter
from PIL import Image, ImageTk
root = Tkinter.Tk()
root.geometry("%dx%d+%d+%d" % (1400,807,0,0))
scrollable = ScrollableFrame(root)
scrollable.pack( fill = Tkinter.BOTH)
image_container = []
for i in range (50):
# Here's the problem! Scrollbar works properly, but after scroll down to 50000
# units, is screen stop at 50000, and when scroll up back to lower than 50000,
# it works again !!!!
img = ImageTk.PhotoImage( Image.open("%s.jpg"%(i)) ) )
image_container.append( Tkinter.Label( scrollable.interior, image = img )
image_container[ len( image_container ) -1 ].image = img
class ScrollableFrame(Tkinter.Frame):
def __init__(self, parent, *args, **kw):
self.set_parameter( kw )
Tkinter.Frame.__init__(self, parent, *args, **kw)
# create a canvas object and a vertical scrollbar for scrolling it
self.scrollbar = Tkinter.Scrollbar(self, orient= self.par["orient"])
self.scrollbar.pack(fill= self.par["scroll_fill"], side= self.par["scroll_side"], expand=Tkinter.FALSE)
self.canvas = Tkinter.Canvas(self, bd=0, highlightthickness=0, bg = self.par["canvas_bg"] )
if self.par["orient"] in ("vertical",Tkinter.VERTICAL):
self.canvas.config(yscrollcommand = self.scrollbar.set )
self.scrollbar.config(command=self.canvas.yview)
else:
self.canvas.config(xscrollcommand = self.scrollbar.set )
self.scrollbar.config(command=self.canvas.xview)
self.canvas.pack(side= self.par["canvas_side"], fill=Tkinter.BOTH, expand=Tkinter.TRUE)
# reset the view
self.canvas.xview_moveto(0)
self.canvas.yview_moveto(0)
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = Tkinter.Frame(self.canvas, bg = self.par["canvas_bg"])
interior_id = self.canvas.create_window(0, 0, window=interior,
anchor="nw")
# track changes to the canvas and frame width and sync them,
# also updating the scrollbar
def _configure_interior(event):
# update the scrollbars to match the size of the inner frame
size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
print size, "interior"
self.canvas.config(scrollregion="0 0 %s %s" % size)
if self.par["orient"] in ("vertical",Tkinter.VERTICAL):
if interior.winfo_reqwidth() != self.canvas.winfo_width():
# update the canvas's width to fit the inner frame
self.canvas.config(width=interior.winfo_reqwidth())
else:
if interior.winfo_reqheight() != self.canvas.winfo_height():
# update the canvas's width to fit the inner frame
self.canvas.config(height=interior.winfo_reqheight())
interior.bind('<Configure>', _configure_interior)
def _configure_canvas(event):
print "canvas"
if self.par["orient"] in ("vertical",Tkinter.VERTICAL):
if interior.winfo_reqwidth() != self.canvas.winfo_width():
# update the inner frame's width to fill the canvas
self.canvas.itemconfigure(interior_id, width=self.canvas.winfo_width())
else:
if interior.winfo_reqheight() != self.canvas.winfo_height():
# update the inner frame's height to fill the canvas
self.canvas.itemconfigure(interior_id, height=self.canvas.winfo_height())
self.canvas.bind('<Configure>', _configure_canvas)
def set_parameter(self, options):
self.par = {"orient": "vertical",
"canvas_bg":"white"}
if "orient" in options.keys():
self.par["orient"] = options["orient"]
options.pop("orient")
if "canvas_bg" in options.keys():
self.par["canvas_bg"] = options["canvas_bg"]
options.pop("canvas_bg")
if self.par["orient"] in ("vertical",Tkinter.VERTICAL):
self.par["scroll_fill"] = Tkinter.Y
self.par["scroll_side"] = Tkinter.RIGHT
self.par["canvas_side"] = Tkinter.LEFT
else:
self.par["scroll_fill"] = Tkinter.X
self.par["scroll_side"] = Tkinter.BOTTOM
self.par["canvas_side"] = Tkinter.TOP
这项工作起初很好,但是当我加载大约40张图像时,框架高度超过50000(单位)并且配置不起作用!只是无法找出这里的错误,是否存在系统限制高度?