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

    frame1 = Frame(new_window)
    framebot = Frame(new_window)

    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__":

    frame1 = Frame(mw)
    framebot = Frame(mw)

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

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

Use destroy instead of quit.
destroy has not been called

to make the call.
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.
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Button clicked not working catlessness 9 947 Jul-30-2021, 07:00 PM
Last Post: deanhystad
  [Tkinter] Modify Class on Button Click KDog 4 1,724 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 3,369 Jan-18-2021, 10:21 PM
Last Post: Serafim
  [Tkinter] Button click problem using OOP JohnB 5 1,014 Oct-21-2020, 12:43 PM
Last Post: JohnB
  [PyQt] Received RunTimeError after script concludes, closing Dialog Window (clicking x-out) skipper0802 0 964 Oct-09-2020, 09:23 PM
Last Post: skipper0802
  tkinter | Button color text on Click Maryan 2 1,207 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 1,876 Dec-16-2019, 04:47 AM
Last Post: woooee
  tkinter window and turtle window error 1885 3 3,234 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 5,158 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [PyQt] Problem how to click a button inside a group box? mart79 2 1,446 Aug-05-2019, 01:21 PM
Last Post: mart79

Forum Jump:

User Panel Messages

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