TonyC
TonyC

Reputation: 11

Tkinter - Redisplaying multiple windows

I am working on an app that creates a main window and 3 other top-level windows with all 4 displayed on screen at the same time.

When the user moves to another app and then comes back to my app I want all 4 windows to be shown on screen again, at the moment only the one window that the focus is given back to is displayed (the others remain hidden behind the other app).

I have tried binding a method to the focus-in event for each window which calls deiconify on each window but this just causes an infinite focus change loop and I can't find a way to stop it.

Have I missed something about how to redisplay all windows?

import tkinter
      
def run():
    root = tkinter.Tk()
    root.geometry("+1+1")
    tkinter.Button(command=root.destroy, text="Quit").pack()
    #Canvases
    t1 = tkinter.Toplevel(root)
    t1.geometry("+1+1")
    t2 = tkinter.Toplevel(root)
    t2.geometry("+265+1")
    t3 = tkinter.Toplevel(root)
    t3.geometry("+1005+1")
    t4 = tkinter.Toplevel(root)
    t4.geometry("+265+770")

    root.mainloop()

def displayAllWindows(widget):
    pass
    
def disable_event():
    pass

if __name__ == '__main__':
    run()

Upvotes: 1

Views: 66

Answers (1)

kpie
kpie

Reputation: 11090

This is something of an ugly implementation but the main point is that you want to bind a function on <FocusIn> which will call lift() on each of your windows. I don't really recommend the global variables but for the purpose of demonstration I didn't really think it was necessary to wrap it up in a class.

import tkinter

windows = []

def handle_focus(event):
    global windows
    windows[0].lift()
    windows[1].lift()
    windows[2].lift()
    windows[3].lift()

def run():
    global windows
    root = tkinter.Tk()
    root.geometry("+1+1")
    tkinter.Button(command=root.destroy, text="Quit").pack()
    # Canvases
    t1 = tkinter.Toplevel(root)
    t1.geometry("+1+1")
    t2 = tkinter.Toplevel(root)
    t2.geometry("+265+1")
    t3 = tkinter.Toplevel(root)
    t3.geometry("+1005+1")
    t4 = tkinter.Toplevel(root)
    t4.geometry("+265+770")
    windows.append(t1)
    windows.append(t2)
    windows.append(t3)
    windows.append(t4)
    t1.bind("<FocusIn>", handle_focus)
    t2.bind("<FocusIn>", handle_focus)
    t3.bind("<FocusIn>", handle_focus)
    t4.bind("<FocusIn>", handle_focus)
    root.mainloop()

def displayAllWindows(widget):
    pass


def disable_event():
    pass


if __name__ == '__main__':
    run()

Upvotes: 3

Related Questions