파이썬 tkinter에서 프레임을 전환하고자 하는데, maximum recursion depth exceeded 에러가 납니다.

조회수 677회
from tkinter import *

class myApp(Tk):

    def changeFrame(self, classParam):
        # if self.nowFrame:
        #     self.nowFrame.destory() # 이부분에서 에러가 발생합니다.
        self.nowFrame = classParam
        self.nowFrame.pack()

    def __init__(self, root):
        myMenu=Menu(root)
        menu1=Menu(myMenu)
        menu1.add_command(label="이전", command=lambda:self.changeFrame(frameClass_1()))
        menu1.add_command(label="다음", command=lambda:self.changeFrame(frameClass_2()))
        myMenu.add_cascade(label="프레임전환", menu=menu1)
        root.config(menu=myMenu)
        root.mainloop()



class frameClass_1(Frame):
    def __init__(self):
        super().__init__()
        Label(self, text="frame1").pack()



class frameClass_2(Frame):
    def __init__(self):
        super().__init__()
        Label(self, text="frame2").pack()

root = Tk()
myApp(root)

이전 메뉴를 선택하면 프레임 1로 전환하고,다음 메뉴를 선택하면 프레임 2로 전환하고자 합니다.

하지만, 메뉴를 선택하면, 아래의 에러가 뜹니다. 어떻게 해결해야 할까요?

[Previous line repeated 989 more times]
RecursionError: maximum recursion depth exceeded

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    스택오버플로우에서 해결했습니다.

    import tkinter as tk
    
    class myApp(): # Tk를 상속할 필요 없음.
    
        def switchFrame(self, classParam):
            if self.nowFrame: # destory가 아니라 destroy임
                self.nowFrame.destroy()
            self.nowFrame = classParam
            self.nowFrame.pack()
    
    
        def __init__(self, root):
            self.nowFrame = None # nowFrame을 초기화 해줘야함.
            myMenu=tk.Menu(root)
            menu1=tk.Menu(myMenu)
            menu1.add_command(label="before", command=lambda:self.switchFrame(frameClass_1()))
            menu1.add_command(label="next", command=lambda:self.switchFrame(frameClass_2()))
            myMenu.add_cascade(label="switch", menu=menu1)
            root.config(menu=myMenu)
            root.mainloop()
    
    
    
    class frameClass_1(tk.Frame):
        def __init__(self):
            super().__init__()
            tk.Label(self, text="frame1").pack()
    
    
    
    class frameClass_2(tk.Frame):
        def __init__(self):
            super().__init__()
            tk.Label(self, text="frame2").pack()
    
    root = tk.Tk()
    myApp(root)
    
    
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)