John Doe John Doe - 7 days ago 6
Python Question

Only close one tkinter window

I can make my program close the whole program whoever I have not yet figured out a way so that when a button is clicked it only closes that window and not the whole program.

from tkinter import *


class First(object):

def __init__(self, master):
self.master = master
self.admin_btn = Button(self.master, text = "First", command = self.second)
self.admin_btn.grid(row = 0, column = 0)

def second(self):
self.w = second(self.master)
self.master.wait_window(self.w.master)


class second(object):

def __init__(self, master):
self.master = Toplevel(master)
self.second = Button(self.master, text = "Second", command = self.third)
self.second.grid(row= 0, column = 0)

def third(self):
self.w = third(self.master)
self.master.wait_window(self.w.master)


class third(object):

def __init__(self, master):
self.master = Toplevel(master)
self.second = Button(self.master, text = "Third")
self.second.grid(row= 0, column = 0)
## The close button, not linked to any command
self.close = Button(self.master, text = "Close")
self.close.grid(row = 1, column = 0)


if __name__ == "__main__":
root = Tk()
first = First(root)
root.mainloop()

Answer

Use self.master.destroy.

self.close = Button(self.master, text = "Close", command=self.master.destroy)