Python Forum
Closing window on button click not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Closing window on button click not working
#1
Hello all,
I am trying to close/destroy a toplevel window via a button.
When I click on the columns_button, a new toplevel window is created with an save_button and exit_button.
When I click the exit_button, the command new_window.destroy closes the window.
When I click the save_button, the program calls the save_columns routine which calls new_window.destroy but the new_window does not close out.
How do I close the new_window from within the save_columns routine.
Thanks in advance.


from tkinter import *
from tkinter import ttk

def get_columns():
    new_window = Toplevel(mw)
    new_window.wm_title("Select Columns")
    new_window.geometry('450x250+250+150')

    frame1 = Frame(new_window)
    framebot = Frame(new_window)
    frame1.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)

    w1 = Label(frame1, text="Select Columns ",font=("Times",16)).pack(side="left")

    exit_button = Button(framebot,text='Exit',font=("Times",16),command=new_window.destroy).pack(side="right")
    save_button = Button(framebot,text='Save/Exit',font=("Times",16),command=lambda:save_columns(new_window)).pack(side="left")

def save_columns(new_window):
    print("Saving Columns")
    new_window.destroy # <<<<<<<< Why does this not destroy the window?

if __name__ == "__main__":
    mw=Tk()
    mw.geometry('450x250+200+150')

    frame1 = Frame(mw)
    framebot = Frame(mw)
    frame1.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)

    w2 = Label(frame1, text="Table Name: ",font=("Times",16)).pack(side="left")
    a2 = ttk.Combobox(frame1,width=40,font=("Times",16))
    a2.pack(side="left")

    columns_button = Button(framebot,text='Columns',font=("Times",16),command=get_columns).pack(side="left")
    quit_button = Button(framebot,text='Exit',font=("Times",16),command=mw.quit).pack(side="right")

    mw.mainloop()
Reply
#2
Use destroy instead of quit.
Reply
#3
destroy has not been called

change
new_window.destroy
to
new_window.destroy()
to make the call.
Reply
#4
Thank you for your responses.
I put parens after the new_window.destroy in my save_columns routine and that fixed the problem.
I tried putting parens after the new_window.destroy on my Exit button command and it failed with a bad window pathname error.
They should fix Python so that is is more consistent.
Thanks.
Reply
#5
function() executes the function. When binding a function to a button you want the button to execute the function, so you pass the function without parens.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 343 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  tkinter - touchscreen, push the button like click the mouse John64 5 746 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  [PyQt] PyQt5 window closing when trying to display a graph bianca 4 1,621 Aug-12-2023, 03:25 PM
Last Post: bianca
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 727 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,382 May-25-2023, 07:37 PM
Last Post: deanhystad
  Figure Gets Larger Every time I click a show button joshuagreineder 2 1,271 Aug-11-2022, 06:25 AM
Last Post: chinky
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,198 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  [PyQt] Button clicked not working catlessness 10 8,085 Oct-21-2021, 12:36 PM
Last Post: catlessness
  [Tkinter] Modify Class on Button Click KDog 4 3,904 May-11-2021, 08:43 PM
Last Post: KDog
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 9 13,417 Jan-18-2021, 10:21 PM
Last Post: Serafim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020