0

Hi i am new to python tkinter programing.I am trying to close current window.But when,I click quit all the windows including the main are quitting. I am using currrentwindow.destory(),but it's closing all .

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

Thanks for the help in advance.

Priyaranjan
  • 407
  • 3
  • 9
  • 16

1 Answers1

1

you are closing the application rather than a window. You need to call destroy on the individual window you want to close. The easiest way to do that in your situation is with a lambda:

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

You should be able to get by with the less confusing:

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

But, it is always good to be careful. Depending on what you end up doing down the road, that could cause very confusing errors for a newbie (see here, and here).

You can then get rid of your close_window function.

Edit:

Correction per @fhdrsdg below -- no need for lambda here:

    B5 = tk.Button(DicWin, text='quit', command=DicWin.destroy)
Community
  • 1
  • 1
Alan Hoover
  • 1,430
  • 2
  • 9
  • 13
  • 1
    When you use a lambda, you need to actually call the function in the lambda, so using `command=lambda: DicWin.destroy()`. However, since you're not passing arguments to the function, there's no need for the lambda. Just use `command=DicWin.destroy`. – fhdrsdg Apr 23 '15 at 20:42