Naman Jain Naman Jain - 4 months ago 8x
Python Question

tkinter maximum recursion depth exceeded while generating an event

I'm trying to bind mouse motions (pressed/unpressed) with some methods.
I tried to handle mouse motions while mouse button is pressed with '' and the other with just ''.
I found that when I just have ..bind('', somemethod1), somemethod1 is called regardless of mouse button press, but when I also have ..bind('', somemethod2), somemethod1 is not called when mouse button was pressed.
Adding 'add='+'' didn't seem to work.

def bind_mouse(self):
self.canvas.bind('<Button1-Motion>', self.on_button1_motion1)
self.canvas.bind('<Motion>', self.on_mouse_unpressed_motion1)

def on_button1_motion1(self, event):

def on_mouse_unpressed_motion1(self, event):

So I instead modified the on_button1_motion1 method as below:

def on_button1_motion1(self, event):

But when I tried this, I got this runtime error:

Traceback (most recent call last):
File "D:/save/WORKSHOP/py/tkinter/Blueprints/Pycrosoft Paintk/", line 107, in
File "C:\Users\smj\AppData\Local\Programs\Python\Python35\lib\", line 1131, in mainloop
RecursionError: maximum recursion depth exceeded

Can anyone explain to me why this is happening?
I know I can solve this problem by just calling on_mouse_unpressed_motion1 method inside on_button1_motion1 method instead of generating an event, but I'd like to know why the other way doesn't work. Thanks


It creates an infinite loop. You are listening for <Button1-Motion>, and when you get it, you create more <Motion> while the Button is pressed (because it's only generated when the Button1 Event is caught) so you're generating another <Button1-Motion> event.. So the function is called again and so on.

 <Motion>:  The mouse is moved with a mouse button being held down. 
To specify the left, middle or right mouse button use
 <B1-Motion>, <B2-Motion> and <B3-Motion> respectively.

From here