Python tkinter closes the previous toplevel when creating a new toplevel

problem description

just learned python GUI, using tkinter

when you use a button of Tk () to open a Toplevel , you can generally open it indefinitely. How to automatically close the toplevel before creating a new project?

my gui looks like this:

()

  1. button(),destroy(), Toplevel,button

  1. one way to realize automation is to check whether the focus is in this window. If not, destroy () , but don"t know how to do it. bind can only be used for controls
.
Jun.22,2022

resolved.
introducing global variables

  stackoverflow  

Menu