简体   繁体   中英

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()

You have a method submit_new_flashcard . When you create the button and assign command=self.submit_new_flashcard the button is created and the command property bound to the method. Then you assign the self.submit_new_flashcard field to be the result of creating that button. When you destroy the button, the reference name is still held in the variable. So in creating the second form you create a button that tries to call the original button name as a function which doesn't do anything but raises a background error.

In short, improved naming and avoiding reuse of the same names in the same scope. eg:

self.submitButton = ttk.Button(..., command=self.submit)

would avoid the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM