您好我是python tkinter编程的新手。我正在尝试关闭当前窗口。但是,当我点击退出所有窗口,包括主要退出。 我正在使用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()
感谢您的帮助。
答案 0 :(得分:1)
您正在关闭应用程序而不是窗口。您需要在要关闭的单个窗口上调用destroy。在你的情况下,最简单的方法是使用lambda:
B5 = tk.Button(DicWin, text='quit', command=lambda win=DicWin:win.destroy)
你应该能够解决这个问题:
B5 = tk.Button(DicWin, text='quit', command=lambda :DicWin.destroy)
但是,小心谨慎总是好的。根据您最终要做的事情,这可能会导致新手出现非常混乱的错误(请参阅here和here)。
然后您可以摆脱close_window
功能。
编辑:
下面的每个@fhdrsdg更正 - 这里不需要lambda:
B5 = tk.Button(DicWin, text='quit', command=DicWin.destroy)