0

I was writing a python program and accidentally called mainloop multiple times. I fixed it but I am still wondering what actually happens when you call it multiple times? This is some of my code (you have to press the button around 100 times):

import tkinter as tk

root = tk.Tk()
root.resizable(False, False)
button = tk.Button(root, text="Press me multiple times for the crash", command=root.mainloop)
button.pack()
root.mainloop()

This is the traceback I got:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
RecursionError: maximum recursion depth exceeded while calling a Python object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283, in mainloop
    self.tk.mainloop(n)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1709, in __call__
    self.widget._report_exception()
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1452, in _report_exception        
    root.report_callback_exception(exc, val, tb)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2093, in report_callback_exception
    import traceback
  File "C:\Program Files\Python37\lib\traceback.py", line 5, in <module>
    import linecache
  File "C:\Program Files\Python37\lib\linecache.py", line 11, in <module>
    import tokenize
  File "C:\Program Files\Python37\lib\tokenize.py", line 94, in <module>
    class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')):
  File "C:\Program Files\Python37\lib\collections\__init__.py", line 397, in namedtuple
    exec(s, namespace)
RecursionError: maximum recursion depth exceeded during compilation

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283, in mainloop
    self.tk.mainloop(n)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1709, in __call__
    self.widget._report_exception()
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1452, in _report_exception
    root.report_callback_exception(exc, val, tb)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2093, in report_callback_exception
    import traceback
  File "C:\Program Files\Python37\lib\traceback.py", line 5, in <module>
    import linecache
  File "C:\Program Files\Python37\lib\linecache.py", line 11, in <module>
    import tokenize
  File "C:\Program Files\Python37\lib\tokenize.py", line 94, in <module>
    class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')):
  File "C:\Program Files\Python37\lib\collections\__init__.py", line 397, in namedtuple
    exec(s, namespace)
RecursionError: maximum recursion depth exceeded during compilation
Fatal Python error: Cannot recover from stack overflow.

Current thread 0x00000e04 (most recent call first):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705 in __call__
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705 in __call__
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705 in __call__
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705 in __call__
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705 in __call__
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • [This](https://stackoverflow.com/a/2711177) will provide some answers to your question. – Thingamabobs Jan 04 '21 at 21:35
  • The problem is what happens if you put `mainloop` as a parameter of `tk.Tk().after` not what happens when just simply call `mainloop` multiple times. In the latter, after the window has been closed, `mainloop()` seems to have no effect and does not bring the window up again. – Countour-Integral Jan 04 '21 at 21:37
  • I made the code simpler and you turns out that you don't even need tkinter's `after` method. But now you have to press the button around 100 times for the error to occur. – TheLizzard Jan 05 '21 at 10:03
  • 1
    Recursion is due to calling mainloop() inside mainloop(). – acw1668 Jan 05 '21 at 13:05
  • Is there a way to handle that error? – TheLizzard Jan 05 '21 at 14:45
  • To fix it, don't call `mainloop()` more than once. I don't understand why you need to call it multiple times. – acw1668 Jan 07 '21 at 09:02
  • Yes but if I call it multiple times (because of a coding mistake), how can I detect it. Last time it happened it took me 3h to find my mistake. – TheLizzard Jan 07 '21 at 20:18

0 Answers0