如何在 python tkinter 中设计退出当前 window 方法而不退出其他 windows?

how to design quit current window method in python tkinter without quitting other windows?

嗨,我是 python tkinter 的新手 programing.I 我正在尝试关闭当前 window.But 时,我单击退出所有 windows 包括主要正在退出。 我正在使用 currrentwindow.destory(),但它正在关闭所有 .

def dic_winn3():
    DicWin = tk.Toplevel(root)
    DicWin.title('operations')
    DicWin.geometry('300x300')
    DicWinButton.config(state='normal')
    L1 = Label(DicWin, text="country")
    L1.pack()
    E1 = Entry(DicWin, bd =20)
    E1.pack()
    L2 = Label(DicWin, text="phone")
    L2.pack()
    E2 = Entry(DicWin, bd =20)
    E2.pack()
    L3 = Label(DicWin, text="customerid")
    L3.pack()
    E3 = Entry(DicWin, bd =20)
    E3.pack()
    L4 = Label(DicWin, text="address")
    L4.pack()
    E4 = Entry(DicWin, bd =20)
    E4.pack()
    L5 = Label(DicWin, text="contact name")
    L5.pack()
    E5 = Entry(DicWin, bd =20)
    E5.pack()
    B3 = tk.Button(DicWin, text='quit', command=close_window)
    B3.pack()


def dic_winn4():
    DicWin = tk.Toplevel(root)
    DicWin.title('operations')
    DicWin.geometry('300x300')
    DicWinButton.config(state='normal')
    L1 = Label(DicWin, text="productid")
    L1.pack()
    E1 = Entry(DicWin, bd =20)
    E1.pack()
    L2 = Label(DicWin, text="orderid")
    L2.pack()
    E2 = Entry(DicWin, bd =20)
    E2.pack()
    L3 = Label(DicWin, text="quantity")
    L3.pack()
    E3 = Entry(DicWin, bd =20)
    E3.pack()
    B4 = tk.Button(DicWin, text='quit', command=close_window)
    B4.pack()

def dic_winn5():
    DicWin = tk.Toplevel(root)
    DicWin.title('operations')
    DicWin.geometry('300x300')
    DicWinButton.config(state='normal')
    L1 = Label(DicWin, text="unitprice")
    L1.pack()
    E1 = Entry(DicWin, bd =20)
    E1.pack()
    L2 = Label(DicWin, text="units in stock")
    L2.pack()
    E2 = Entry(DicWin, bd =20)
    E2.pack()
    L3 = Label(DicWin, text="product name")
    L3.pack()
    E3 = Entry(DicWin, bd =20)
    E3.pack()
    L4 = Label(DicWin, text="product id")
    L4.pack()
    E4 = Entry(DicWin, bd =20)
    E4.pack()
    B5 = tk.Button(DicWin, text='quit', command=close_window)
    B5.pack()


def dic_winn6():
    DicWin = tk.Toplevel(root)
    DicWin.title('operations')
    DicWin.geometry('300x300')
    DicWinButton.config(state='normal')
    L1 = Label(DicWin, text="supplierid")
    L1.pack()
    E1 = Entry(DicWin, bd =20)
    E1.pack()
    L2 = Label(DicWin, text="company name")
    L2.pack()
    E2 = Entry(DicWin, bd =20)
    E2.pack()
    B6 = tk.Button(DicWin, text='quit', command=close_window)
    B6.pack()

#def quit_win():
    #DicWin.destroy()
    #DicWinButton.config(state='normal')

def close_window (): 
    root.destroy()

#def close_window1 (): 
    #DicWin1.destroy()

QuitButton = tk.Button(root, text='Quit', command=close_window)
QuitButton.pack()
#button = tk.Button (root, text = "Good-bye.", command = close_window)
#button.pack()
DicWinButton = tk.Button(root, text='insert', command=dic_win)
DicWinButton.pack()
DicWinButton = tk.Button(root, text='update', command=dic_win)
DicWinButton.pack()
DicWinButton = tk.Button(root, text='delete', command=dic_win)
DicWinButton.pack()


root.mainloop()

提前感谢您的帮助。

您正在关闭应用程序而不是 window。您需要对要关闭的个人 window 调用销毁。在您的情况下,最简单的方法是使用 lambda:

B5 = tk.Button(DicWin, text='quit', command=lambda win=DicWin:win.destroy)

你应该能够比较容易理解的:

    B5 = tk.Button(DicWin, text='quit', command=lambda :DicWin.destroy)

但是,小心点总是好的。根据您最终在路上所做的事情,这可能会给新手带来非常混乱的错误(请参阅 here, and here)。

然后您可以删除 close_window 函数。

编辑:

根据下面的@fhdrsdg 进行更正——这里不需要 lambda:

    B5 = tk.Button(DicWin, text='quit', command=DicWin.destroy)