0

I want to close the Toplevel (top2 in this case) window after I press ok. I've tried using destroy() method but when I bind it to ok button and click on search customer it says that cannot bind as button has already been destroyed(The toplevel window doesn't open). How can it be destroyed if I haven't clicked on ok yet?

class layout:
    def __init__(self, master):
        self.frame = tk.Frame(master, height=600, width=720, bg='black')
        self.var1 = tk.IntVar()
        self.var1.set("")
        self.varid = tk.IntVar()
        self.varid.set("")
        self.var2 = tk.StringVar()
        self.var3 = tk.StringVar()
        self.var4 = tk.StringVar()
        self.entry1 = tk.Entry(self.frame, font=("Franklin Gothic", "15", " roman"), width=40, textvariable=self.var1)
        self.entry2 = tk.Entry(self.frame, font=("Franklin Gothic", "15", " roman"), width=40, textvariable=self.var2)
        self.entry3 = tk.Entry(self.frame, font=("Franklin Gothic", "15", " roman"), width=40, textvariable=self.var3)
        self.entry4 = tk.Entry(self.frame, font=("Franklin Gothic", "15", " roman"), width=40, textvariable=self.var4)
        self.button1 = tk.Button(self.frame, text="Quit", command=self.frame.quit)
        self.button3 = tk.Button(self.frame, text="Search Customer")

    def search_click(self, event):      
        top2 = tk.Toplevel()
        top2.minsize(320, 210)
        top2.maxsize(320, 210)
        lblid = tk.Label(top2, text='Enter ID', bg='white', font=("Franklin Gothic", "12", " bold roman"))
        lblid.place(x=20, y=60)
        entryid = tk.Entry(master=top2, font=("Franklin Gothic", "12", " roman"), textvariable=self.varid)
        entryid.place(x=90, y=60)
        btnid = tk.Button(top2, text='OK')
        btnid.place(x=80, y=80)
        btnid.bind('<Button-1>', self.search)
        btnid.bind('<ButtonRelease-1>', top2.destroy())
        top2.configure(bg='white')
        top2.title('Search Customer')

When I click on search customer it shows an error:

Bad window path name !toplevel.!Button.
  • Thank you for pinning the answer. It solved my problem. I had to use destroy in a new fuction and then bind it to the button, not directly binding it to destroy. – Shobhit Gupta Jul 19 '19 at 10:17

1 Answers1

-1

Try to call top2.quit() before calling top2.destroy(). So in your case, bind btnid to the quit() method of top2, and after the mainloop of top2 ends, you can destroy it.

wserr
  • 436
  • 1
  • 3
  • 12