from tkinter import *
def on_focus_in_win0(event):
if win1exist == 1:
win1.state('normal')
win1.lift()
if win2exist == 1:
win2.state('normal')
win2.lift()
def on_focus_in_win1(event):
win0.state('normal')
win1.state('normal')
win1.lift()
if win2exist == 1:
win2.state('normal')
win2.lift()
def on_focus_in_win2(event):
win0.state('normal')
win1.state('normal')
win1.lift()
win2.state('normal')
win2.lift()
def destroy_win1(event):
global win1exist, win2exist
win1exist = 0
win2exist = 0
win2.destroy()
def show_win1():
global win1, win1exist
if win1exist == 1:
win1.lift()
win1.after(1, lambda: win1.focus_force())
return
win1exist = 1
win1 = Toplevel()
win1.geometry("400x400")
win1.title('win1')
win1.lift()
win1.after(1, lambda: win1.focus_force())
win1.bind("<Destroy>", destroy_win1)
win1.bind("<FocusIn>", on_focus_in_win1)
but02 = Button(win1, text="Show Win2", fg="black", font="none 15 bold", command=show_win2)
but02.pack()
def destroy_win2(event):
global win2exist
win2exist = 0
print ( win2exist )
def show_win2():
global win2, win2exist
if win2exist == 1:
win2.lift()
win2.after(1, lambda: win2.focus_force())
return
win2exist = 1
win2 = Toplevel()
win2.geometry("300x300")
win2.title('win2')
lab1 = Label(win2, text="Last window", font="none 10 bold")
lab1.pack()
win2.lift()
win2.after(1, lambda: win2.focus_force())
win2.bind("<Destroy>", destroy_win2)
win2.bind("<FocusIn>", on_focus_in_win2)
def task( event ):
global counter
counter = counter + 1
print (counter)
win0.state('normal')
if win1exist == 1:
win1.state('normal')
if win2exist == 1:
win2.state('normal')
win0 = Tk()
win0.title('win0')
win0.geometry("500x500")
win1exist = 0
win2exist = 0
counter = 0;
but01 = Button(win0, text="Show Win1", fg="black", font="none 15 bold", command=show_win1)
but01.pack()
win0.bind("<FocusIn>", on_focus_in_win0)
mainloop()