月曜日に Python でプログラミングを始めました。楽しく学べました。しかし、tkinterメニューを切り替えるときに再帰を回避する方法を理解しようとして立ち往生しています! これは非常に基本的な質問であると確信しており、この件に関する私の無知を許容していただきありがとうございますが、他の場所で答えを見つけることができませんでした.
私が今やっていることは、最終的に、エラーが発生することです: 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