Python 当Tkinter帧类太长时,事件“configure”停止跟踪它

Python 当Tkinter帧类太长时,事件“configure”停止跟踪它,python,tkinter,Python,Tkinter,嘿,伙计们,我正在使用Tkinter开发一个图像读取GUI,试图在画布中自动调整框架的大小,当新图像添加到框架中时,它的滚动条应该会更新。 我在web上找到了一个非常有用的示例,并将其修改为更一般的示例: import Tkinter from PIL import Image, ImageTk root = Tkinter.Tk() root.geometry("%dx%d+%d+%d" % (1400,807,0,0)) scrollable = ScrollableFrame(roo

嘿,伙计们,我正在使用Tkinter开发一个图像读取GUI,试图在画布中自动调整框架的大小,当新图像添加到框架中时,它的滚动条应该会更新。 我在web上找到了一个非常有用的示例,并将其修改为更一般的示例:

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个单位,配置不起作用!只是找不出这里有什么问题,有系统限制高度吗?

您没有正确突出显示所有代码。抱歉,我现在添加了更多说明。