Search code examples
pythonpython-3.xtkintertkinter-entry

Tkinter : wait for user click


How can I spawn a window, and halt the execution of the GUI until this window is closed by the user?


Solution

  • That's exactly what functions from the tkinter.messagebox submodule will do. These will spawn a dialog, and halt the execution until closed.

    For instance, the showinfo function will spawn a window with the first parameter as title and the second as message. Until the window is closed, the remaining of the GUI will not be interactable.

    Here is an example demonstrating this.

    import tkinter as tk
    import tkinter.messagebox as tkmb
    
    root = tk.Tk()       
    button = tk.Button(
        root,
        text="Spawn a dialog",
        command=lambda: tkmb.showinfo(
            "Information",
            "Please close this window or press OK to continue"))
    button.pack()
    
    root.mainloop()
    

    When the button is clicked, a window spawns. As long as this window is open, the button will not be clickable once again.