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
  [Tkinter] Button click problem using OOP JohnB 5 233 Oct-21-2020, 12:43 PM
Last Post: JohnB
  [PyQt] Received RunTimeError after script concludes, closing Dialog Window (clicking x-out) skipper0802 0 194 Oct-09-2020, 09:23 PM
Last Post: skipper0802
  tkinter | Button color text on Click Maryan 2 225 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 999 Dec-16-2019, 04:47 AM
Last Post: woooee
  tkinter window and turtle window error 1885 3 1,630 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 2,265 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [PyQt] Problem how to click a button inside a group box? mart79 2 789 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 1,282 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [split] Closing a window but not the whole program scriptdrache 1 551 Jun-25-2019, 03:43 PM
Last Post: joe_momma
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 1,271 May-12-2019, 08:17 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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