I have a main tkinter window which launches multiple pop up windows. I am trying to close all windows by pressing the cross in the pop up window or by pressing quit. I have the following code which closes only the pop up window by pressing the quit or cross, but does not close the main window.How to refactor the code to achieve the desired output.
import tkinter as tk
from tkinter import *
class myclass:
def __init__(self,master):
self.master = master
self.button = tk.Button(self.master, text = "Function", command = self.launchFunc)
self.button.place(width=160,height=50, x=20, y=20)
self.quit = tk.Button(self.master, text = "Quit", command = self.close_windows)
self.quit.place(width=160,height=50, x=20, y=80)
def close_windows(self):
self.master.destroy()
def launchFunc(self):
self.top = tk.Toplevel(self.master)
self.top.transient(self.master)
self.app = funcClass(self.top)
class funcClass:
def __init__(self, master):
self.master1 = master
self.quit = tk.Button(self.master1, text = "Quit", command = self.close_windows)
self.quit.place(width=160,height=50, x=20, y=20)
def close_windows(self):
self.a = myclass(self.master1)
self.a.close_windows()
if __name__ == "__main__":
root = tk.Tk()
myclass = myclass(root)
root.mainloop()
Please guide me. Thank you
funcClass.close_windows()creating a new instance ofmyclass? You already have such an instance (inself.master1), and it has the rightmastervalue to close the root window.