我在星期一开始使用Python进行编程。我很喜欢学习它。但我坚持试图了解如何在tkinter菜单之间切换时避免递归!我相信这是一个非常基本的问题,我感谢你们对这个问题的无知,但我一直无法在其他地方找到答案。Tkinter - RuntimeError:超过最大递归深度
我现在做什么,最终,给我的错误:RuntimeError:最大递归深度,同时调用Python对象
这是超出我目前使用的模式。更新:下面的代码现在是一个完整的,孤立的副本,再现我面临的问题! :D
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()
它一切正常,直到它从最大递归深度的不可避免的失败。如果任何人都可以提出一个更好的做法,或者有一个更好的做法的例子,我很想学习。 PS:我已经看过并试图增加递归深度,但我觉得这是一个穷人的解决方案,以解决我的方法的根本问题。
谢谢大家。
按照要求,这里是回溯:
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
你能发布stacktrace吗?需要知道哪个函数导致递归深度异常。 – 2012-04-06 05:30:54
我已根据请求发布追踪。 :) – 2012-04-06 05:57:05