Tkinter: Exception RuntimeError: 'maximum recursion depth exceeded'
odt at dtrx.de
Wed Oct 13 09:18:47 CEST 2010
Jeff Hobbs <jeff.hobbs at gmail.com>:
> On Oct 12, 9:43 am, o... at dtrx.de (Olaf Dietrich) wrote:
>> After some somewhat heavy mouse action inside the
>> canvas (with the left button pressed), the application throws:
>> | Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x19c0998>> ignored
>> | Exception in Tkinter callback
>> (and similiar ones)
>> This error can be provoked faster by setting the recursion limit
>> to lower values (e.g. 400 as in the comment above).
>> Is there anything I can do (apart from increasing the recursion
>> limit) to avoid this exception? Can I improve anything in the
>> script above to make the whole thing more robust?
> It seems very heavy-handed to create 1-pixel images for drawing onto
> the canvas. Any reason not to use something lighter weight?
The example was _heavily_ simplified; originally, there was
a background (e.g. gray-scale) image and I was drawing lines or
other shapes onto that background that should appear in color
and semi-transparent on top of the background. Additionally,
both background and foreground can be zoomed and scrolled, and
there should be a pixel-by-pixel correspondence between the
(zoomed) background pixels and the semi-transparent shapes
in the foreground (as opposed to overlayed vector graphics).
I could not find a more light-weight solution for these
> I suspect the "self.root.update()" is the problem. Try
> update_idletasks() instead, or to even avoid it if possible. You
> don't want to call update in the event loop, because you are likely
> reprocessing from the same call, causing the recursion.
Indeed, Tk/Toplevel/Canvas.update() seems to be at the
center of the problem.
If I replace update() by update_idletasks(), the problem
disappears, but unfortunately, considerably fewer events
are recorded on the canvas (when connecting the pixels with
lines, the lines become much longer with update_idletasks()
than with update()). If I remove both update() and
update_idletasks(), things work similarly as with
update_idletasks() (the display is only marginally slower
than with update_idletasks()).
I wonder if there is any compromise between update()
* update(): smooth lines but recursion exception
* update_idletasks(): non-smooth lines without exceptions
Thanks a lot for your help
More information about the Python-list