How to bring a dialog box to appear at the front in a Tkinter module of Python?


Python Tkinter has many built-in functions and methods that can be used to develop fully functional desktop applications.

The role of dialog boxes is to create a temporary window to ask and retrieve the user input. The dialog box can contain any additional information, for example, asking user permission to execute a specific task, opening and performing other threaded applications, and much more.

Tkinter provides many built-in libraries like messagebox, simpledialog and filedialog library to implement dialog boxes applications. You can customize the message and options on the basis of your application needs.

Bringing the dialog box to the front blocks all the other user operations and asks the user to take action on dialog boxes. The following example demonstrates how to go about it.


In this example, we will create a button that would trigger a dialog box asking the user to take an action. When the dialog box appears at the front, the user cannot perform any other task until the dialog box is not closed.

# Import required libraries
from tkinter import *
from tkinter import messagebox

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.title("Dialog box Example")

# Define a function
def click_me():
   top=messagebox.askokcancel("Question","Do you want to cancel?")

# Create a button to trigger the dialog box
button= Button(win, text= "Click Me", command= click_me)



On execution, it will first produce the following window −

On clicking the button, it will display a messagebox and prompt the user to take an action. As long as the messagebox is active, the parent window at the background will remain inactive.

Updated on 22-Dec-2021 08:15:45