'how to close tkinter window in a called function?

I am a python beginner and look for some help about my code.

The 1st code will create a Tk window and call 2nd code. Then the 2nd code will create a Tk window with a button, which is expected to close both 2 Tk windows by clicking.

However, I can only close the 2nd Tk window. Pls help to check how to fix the code.

Here is the 1st code:

import tkinter as tk
import tkinter_try2

def function1():
    root1=tk.Tk()
    root1.title("function1")
    root1.geometry('500x100')
    tkinter_try2.function2()
    root1.mainloop()   
    
if __name__  == '__main__':
    function1()

the 2nd code:

import tkinter as tk


def function2():
    root2=tk.Tk()
    root2.title("function2")
    root2.geometry('500x100')
    bt_1 = tk.Button(root2, text="close function1 & 2",command= lambda:[root2.destroy,root1.destroy])
    bt_1.pack()
    root2.mainloop()

if __name__  == '__main__':
    function2()
    

Thank you for any advises.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source