完成功能后如何销毁顶层视窗?

时间:2019-07-18 21:11:34

标签: python-3.x tkinter toplevel

我想在按OK后关闭Toplevel(在这种情况下为top2)窗口。 我尝试使用destroy()方法,但是当我将其绑定到“确定”按钮并单击“搜索客户”时,它说无法绑定,因为按钮已被破坏(顶层窗口未打开)。如果我还没有点击确定,怎么销毁它?

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

当我点击搜索客户时,它会显示错误:

Bad window path name !toplevel.!Button.

1 个答案:

答案 0 :(得分:-1)

尝试在致电top2.quit()之前先致电top2.destroy()。因此,在您的情况下,将btnid绑定到top2的quit()方法上,并在top2的主循环结束后将其销毁。