Note that there are some explanatory texts on larger screens.

plurals
  1. POTkinter - RuntimeError: maximum recursion depth exceeded
    primarykey
    data
    text
    <p>I started programming in Python on Monday. I have enjoyed learning it. But I am stuck trying to understand how to avoid recursion when switching between tkinter menus! I am sure this is a very basic question, and I appreciate you tolerating my ignorance on this subject, but I have been unable to find an answer elsewhere.</p> <p>What I am now doing is, eventually, giving me the error: RuntimeError: maximum recursion depth exceeded while calling a Python object</p> <p>This is the pattern I am currently using. UPDATED: The code below is now a full, isolated copy reproducing the problem I am facing! :D</p> <pre><code>from tkinter import * def mainmenu(): global frame, root frame.destroy() frame = Frame() frame.pack() button1 = Button(frame, text="anothermenulikethis", command = anothermenulikethis) button2 = Button(frame, text="anothermenulikethis", command = anothermenulikethis) button3 = Button(frame, text="mainmenu", command = mainmenu) button1.pack(side=LEFT) button2.pack(side=LEFT) button3.pack(side=LEFT) root.mainloop() def anothermenulikethis(): global frame, root frame.destroy() frame = Frame() frame.pack() button1 = Button(frame, text="mainmenu", command = mainmenu) button2 = Button(frame, text="mainmenu", command = mainmenu) button3 = Button(frame, text="anothermenulikethis", command = anothermenulikethis) button1.pack(side=LEFT) button2.pack(side=LEFT) button3.pack(side=LEFT) root.mainloop() root = Tk() root.title("Recursive Menu Problem Isolation") root.geometry("1200x600") frame = Frame() mainmenu() </code></pre> <p>And it all works fine, until its inevitable failure from maximum recursion depth. If anyone can suggest a better way of doing things, or has a link to an example of a better way of doing this, I am eager to learn.</p> <p>PS: I have looked at and tried increasing the recursion depth, but I feel that is a poor man's solution to what is a fundamental problem with my approach.</p> <p>Thank you in advance, everyone.</p> <p>As requested, here is the traceback:</p> <pre><code>Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/tkinter/__init__.py", line 1399, in __call__ return self.func(*args) File "/Users/diligentstudent/Desktop/menutest.py", line 11, in mainmenu button1 = Button(frame, text="anothermenulikethis", command = anothermenulikethis) File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/tkinter/__init__.py", line 2028, in __init__ Widget.__init__(self, master, 'button', cnf, kw) File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/tkinter/__init__.py", line 1958, in __init__ (widgetName, self._w) + extra + self._options(cnf)) File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/tkinter/__init__.py", line 1043, in _options v = self._register(v) File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/tkinter/__init__.py", line 1079, in _register f = CallWrapper(func, subst, self).__call__ RuntimeError: maximum recursion depth exceeded </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload