'The second time I use a ttk button (in root) to create a toplevel window, the toplevel window cannot be destroyed

Basically, I have a button in root which creates a Toplevel. Then in Toplevel, I also have a ttk.Button, which when clicked will destroy the Toplevel. I am using .destroy(), NOT .quit() or any other wrong commands, which is evident as the first time I create the Toplevel with my root button, my Toplevel button WILL destroy the Toplevel.

However, the second time I click the root button to re-create the Toplevel (in other words, to re-create the Toplevel), the Toplevel will successfully be recreated, but the Toplevel button will not destroy the Toplevel, and I can't understand why. Here is my code (I left in all the additional widgets because when I don't include them, the destroying of the Toplevel works just fine):

from tkinter import *
from tkinter import ttk

class App():
    def __init__(self,root):
        root.title('My Flashcards')
        root.resizable(False,False)

        button_create = ttk.Button(root,text='Create New',command=self.create_new).grid(column=1,row=2)

    def create_new(self):
        self.create_branch = Toplevel()
        self.create_branch.resizable(False,False)
        self.create_branch.title('Create new flashcards')

        ttk.Label(self.create_branch,text='CREATE A NEW FLASHCARD HERE:').grid(column=1,row=1,pady=(10,10),padx=(10,10))
        
        ttk.Label(self.create_branch,text='Type the question:').grid(column=1,row=4,padx=(10,10),pady=(10,10))        
        self.create_question = Entry(self.create_branch,width=70)
        self.create_question.grid(column=1,row=5,padx=(10,10))
        ttk.Label(self.create_branch,text='Type the answer:').grid(column=1,row=6,padx=(10,10),pady=(10,10))        
        self.create_answer = Text(self.create_branch)
        self.create_answer.grid(column=1,row=7,padx=(10,10))
        self.submit_new_flashcard = ttk.Button(self.create_branch,text='Submit',command=self.submit_new_flashcard)
        self.submit_new_flashcard.grid(column=1,row=8,pady=(10,10))

    def submit_new_flashcard(self):
        #Do some things here with self.create_answer.get() and self.create_question.get()
        self.create_branch.destroy()

if __name__ == "__main__":
    root = Tk()
    App(root)
    root.mainloop()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source