tk.Toplevel – Stay above parent and wait

When a toplevel window is shown, we can have it stay on top of the parent window and not allow any interaction with the parent window until the toplevel window has been closed.

import tkinter as tk
from tkinter import ttk


root = tk.Tk()
root.title("Parent Window")


def on_open_second_window_clicked():
    second_window = tk.Toplevel(master=root)
    second_window.title("Second Window")

    # Causes the second window to be centered in the parent window
    # amongst other things like: if the parent window gets minimized,
    # the second window will get minimized too.
    second_window.transient(root)

    # In some cases, updating idle tasks is required
    # before doing a grab_set(). Without it, it may show an exception
    # when attempting to use grab_set()
    second_window.update_idletasks()

    # Causes the second window to be on top of the parent window,
    # and it won't allow any interaction with the parent window.
    second_window.grab_set()

    # This will cause this function to not return until
    # the second window has been closed (useful for when waiting
    # for the user's input).
    second_window.wait_window(second_window)


btn_open_second_window = ttk.Button(root,
                                    text="Open Second Window",
                                    command=on_open_second_window_clicked)
btn_open_second_window.pack()


root.mainloop()