In this Tkinter tutorial we will explain how you can close your TopLevel Window by calling the appropriate functions. People usually mess this part up, either because the concept of Multiple Windows in Tkinter confuses them, or they end up quitting the entire Tkinter application, instead of just the TopLevel window.
Closing a TopLevel Window in Tkinter
First and foremost, you can always close your Top Level Window by pressing the exit/quit icon located at the top-right corner of the window. But of course, if you are reading this, you want a way of doing it through program code.
There is a very simple technique to “destroying” widgets in Tkinter, that involves making a single function call. This can be used to destroy regular widgets (such as a button or label) and even windows (such as TopLevel). This function is called “destroy” and it is available as a method in just about every Tkinter widget.
The below code features a simple example, where we have a Button located in our TopLevel window which triggers a “destroy” function call. Try running the code for yourself!
from tkinter import *
class MainWindow:
def __init__(self, master):
mainframe = Frame(master, width = 300, height = 200)
button = Button(mainframe, text="Open Window",
command=self.openWindow)
button.place(x = 100, y = 80)
mainframe.pack()
def openWindow(self):
win = ExtraWindow()
class ExtraWindow:
def __init__(self):
top = Toplevel()
subframe = Frame(top, width = 200, height = 150)
button = Button(top, text="Destroy Window", command=top.destroy)
button.place(x = 50, y = 50)
subframe.pack()
root = Tk()
window = MainWindow(root)
root.mainloop()
Also bear in mind that when you close the Main Window (the “root” program), the top level window will close as well.
Here is another variation of the above code, where we destroy the TopLevel Window using destroy()
from the Main Window. It’s more or less the same concept.
from tkinter import *
class MainWindow:
def __init__(self, master):
self.win = None
mainframe = Frame(master, width = 300, height = 200)
button = Button(mainframe, text="Open Window",
command=self.openWindow)
button.place(x = 100, y = 60)
button = Button(mainframe, text="Close Window",
command=self.closeWindow)
button.place(x = 100, y = 120)
mainframe.pack()
def openWindow(self):
self.win = ExtraWindow()
def closeWindow(self):
if self.win != None:
self.win.top.destroy()
self.win = None
class ExtraWindow:
def __init__(self):
self.top = Toplevel()
subframe = Frame(self.top, width = 200, height = 150)
subframe.pack()
root = Tk()
window = MainWindow(root)
root.mainloop()
If you are interested in learning more about TopLevel Windows, refer to this interesting tutorial. It explains how to transfer variables and data between the Main Window and a TopLevel Window.
This marks the end of the “How to Close TopLevel Window in Tkinter” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.