How do I close a tkinter window?
You should use destroy()
to close a Tkinter window.
from Tkinter import *
#use tkinter instead of Tkinter (small, not capital T) if it doesn't work
#as it was changed to tkinter in newer Python versions
root = Tk()
Button(root, text="Quit", command=root.destroy).pack() #button to close the window
root.mainloop()
Explanation:
root.quit()
The above line just bypasses the root.mainloop()
, i.e., root.mainloop()
will still be running in the background if quit()
command is executed.
root.destroy()
While destroy()
command vanishes out root.mainloop()
, i.e., root.mainloop()
stops. <window>.destroy()
completely destroys and closes the window.
So, if you want to exit and close the program completely, you should use root.destroy()
, as it stops the mainloop()
and destroys the window and all its widgets.
But if you want to run some infinite loop and don't want to destroy your Tkinter window and want to execute some code after the root.mainloop()
line, you should use root.quit()
. Example:
from Tkinter import *
def quit():
global root
root.quit()
root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
root.mainloop()
#do something
See What is the difference between root.destroy() and root.quit()?.
How do I close a Tkinter Window when a new Tkinter window opens?
Create empty window and run mainloop() (event loop) to keep it open
import tkinter as tk
window = tk.Tk()
window.mainloop()
To close it you can use standard button [x] on title bar.
Or you would need to put button in window which runs window.destroy()
to close it.
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text='CLOSE', command=window.destroy)
button.pack()
window.mainloop()
BTW: in command=
it has to be window.destroy
without ()
. It is "callback"
- name of function which will be executed when you press button.
In this example button runs function which closes first window and create second window.
import tkinter as tk
# --- functions ---
def second_window():
window.destroy()
second = tk.Tk()
button = tk.Button(second, text='CLOSE SECOND', command=second.destroy)
button.pack()
second.mainloop()
# --- main ---
window = tk.Tk()
button = tk.Button(window, text='CLOSE FIRST', command=second_window)
button.pack()
window.mainloop()
Previous version opens second window only if you use tk.Button()
but it doesn't do it when you press [x]
. You need
window.protocol("WM_DELETE_WINDOW", second_window)
to run second_window
when you press [x]
import tkinter as tk
# --- functions ---
def second_window():
window.destroy()
second = tk.Tk()
button = tk.Button(second, text='CLOSE SECOND', command=second.destroy)
button.pack()
second.mainloop()
# --- main ---
window = tk.Tk()
button = tk.Button(window, text='CLOSE FIRST', command=second_window)
button.pack()
window.protocol("WM_DELETE_WINDOW", second_window)
window.mainloop()
You can even run code which close and create again the same window
import tkinter as tk
# --- functions ---
def first_window():
global window
if window:
window.destroy()
#window = None
window = tk.Tk()
button = tk.Button(window, text='CLOSE FIRST', command=first_window)
button.pack()
window.protocol("WM_DELETE_WINDOW", first_window)
window.mainloop()
# --- main ---
window = None
first_window()
In this example I use window = None
to check if window exists and close it. You can use this method to close first window when you create new window.
In this example I use first_window = None
and second_window = None
so I can check if other window is open and close it.
import tkinter as tk
# --- functions ---
def open_second_window():
global first_window
global second_window
if first_window:
first_window.destroy()
first_window = None
second_window = tk.Tk()
button = tk.Button(second_window, text='OPEN FIRST', command=open_first_window)
button.pack()
second_window.protocol("WM_DELETE_WINDOW", open_first_window)
second_window.mainloop()
def open_first_window():
global first_window
global second_window
if second_window:
second_window.destroy()
second_window = None
first_window = tk.Tk()
button = tk.Button(first_window, text='OPEN SECOND', command=open_second_window)
button.pack()
first_window.protocol("WM_DELETE_WINDOW", open_second_window)
first_window.mainloop()
# --- main ---
first_window = None
second_window = None
open_first_window()
But I don't know why you what to close one window and open another. You can hide first window if later you want to go back to first window. Or you can keep widgets in Frame()
and remove/this frame to show other frame.
Can't properly close a GUI window in tkinter
Let's summarize the two immediate mistakes you made. You had your button handlers call close_window
, which called window.quit()
. window
here is a global that refers to your main window, so closing that window kills the app. You need to close the calling dialog window. Second, quit
on the main window quits the app. You need destroy
to simply close the window.
But there are bigger issues we cannot solve. Your dialog functions do nothing. Both buttons simply close the dialog window, without saving the selection that the user made, and without returning any result. You still have a lot of code to write, and we haven't written it here.
def close_window(dlg):
dlg.destroy()
def open_win_delete():
new= Tk()
new.geometry("750x250")
new.title("New Window")
#Create a Label in New window
Label(new, text="Are you sure you want to delete this friend?", font=('Helvetica 17 bold')).grid(row=0,column=2,sticky=NE)
Button(new, text="Yes",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=2,sticky=NE)
Button(new, text="No",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=3,sticky=NE)
def open_win_block():
new= Tk()
new.geometry("750x250")
new.title("New Window")
#Create a Label in New window
Label(new, text="Are you sure you want to block this friend?", font=('Helvetica 17 bold')).grid(row=0,column=2,sticky=NE)
Button(new, text="Yes",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=2,sticky=NE)
Button(new, text="No",width=6, font="none 12 bold", command=lambda: close_window(new)) .grid(row=1,column=3,sticky=NE)
How can I close this tkinter window after pressing the OK button?
I think you have to include master.destroy()
after printing:
from tkinter import *
OPTIONS = [
"Physician 1",
"Physician 2",
"Physician 3"
]
master = Tk()
variable = StringVar(master)
variable.set(OPTIONS[0]) # default value
w = OptionMenu(master, variable, *OPTIONS)
w.pack()
def ok():
physician_name=variable.get()
print (physician_name)
master.destroy()
button = Button(master, text="OK", command=ok)
button.pack()
mainloop()
How to close the Tkinter window when the mouse clicked away from the Tkinter window?
You can still bind <FocusOut>
on root window, but you need to check:
- whether the widget that trigger this event is root window
- no other widget in this root window getting the focus:
def lossfocus(event):
if event.widget is root:
# check which widget getting the focus
w = root.tk.call('focus')
if not w:
# not widget in this window
root.destroy()
How do you close all Tkinter windows when specific window closes?
I am assuming that your other scripts have individual instances of Tk()
, their own mainloop()
and are not under a function, if that is the case, you can have all the code in your files under a function and use Toplevel()
, example, file1
should look like
def something():
window=Toplevel()
#Rest of the code
And similarly file2
, after that in your main program you could do something like this
from tkinter import *
import file1, file2
root = Tk()
root.geometry("600x600")
def newWindowImport():
file1.something()
def newWindowImport2():
file2.something()
newWindow = Button(text="new window", command=newWindowImport)
newWindow.pack()
newWindow2 = Button(text="new window", command=newWindowImport2)
newWindow2.pack()
# Here is there a way so that when I exit it destroys the Main Menu as well as the opened windows
exitBtn = Button(text="Exit", command=root.destroy)
root.mainloop()
You could also let go of the functions and make these changes to have it shorter
newWindow = Button(text="new window", command=file1.something)
newWindow.pack()
newWindow2 = Button(text="new window", command=file2.something)
newWindow2.pack()
The reason for your approach not working would be that each file had it's own mainloop()
and hence they couldn't be destroyed when you called root.destroy
in the main code.
Also note that I have removed the parentheses ()
from the command=root.destroy
otherwise the it will be called as soon as the program initializes.
EDIT : As also suggested by @martineau in the comments, it's better to use .pack()
on the Button
instances separately as it provides more flexibility in using the instance later in the program, as opposed to having them hold the value None
which is the return from .pack()
How to close a single window in Tkinter without closing the main window?
As far as I understand (I cannot reproduce your code) what you might want to close is w
, not root
, as that last one is your main loop.
Replace:
root.destroy()
For:
w.destroy()
Closing current window when opening a new window in tkinter python
You can destroy the previous window and start a new window using Tk class
Here is an example
from tkinter import *
from tkinter.ttk import Button
root = Tk()
root.title("title")
root.geometry("800x500")
def window2():
root.destroy()
window2_main = Tk()
Label(window2_main, text="Bye Bye").pack()
window2_main.mainloop()
a = Button(text="Click This", command=window2)
a.pack()
root.mainloop()
Related Topics
Python Popen Command. Wait Until the Command Is Finished
How to Use Cookies in Python Requests
Blocking and Non Blocking Subprocess Calls
Differencebetween List and List[:] in Python
Matplotlib: Annotating a 3D Scatter Plot
How to Pass Optional Parameters to a Function
Applying Udfs on Groupeddata in Pyspark (With Functioning Python Example)
Possible to Share In-Memory Data Between 2 Separate Processes
Pythonic Way of Checking If a Condition Holds for Any Element of a List
Import Pandas Dataframe Column as String Not Int
How to Change the Default MySQL Connection Timeout When Connecting Through Python
Multiprocessing Example Giving Attributeerror
Get Screenshot on Windows with Python
Django: Deploying an Application on Heroku with SQLite3 as the Database