Introduction
Dialog boxes are a commonly used GUI element to provide feedback to the user and also to prompt the user for information or to take an action. Some examples of common dialogs are:
- A simple message: "Press OK to continue"
- Ask for "OK or cancel"
- Ask for "Yes, no or cancel"
We will look at several different methods for creating dialog boxes in Python including cross-platform options like tkinter and PyAutoGUI, as well as a couple Windows specific options.
Using PyAutoGUI (cross-platform)
This is the method I recommend as it is the simplest to use. It is also the most limited option though. PyAutoGUI uses tkinter under the hood, but provides a nice interface.
# python -m pip install pyautogui
import pyautogui
# Examples:
pyautogui.alert('Just a notification', "Title") # always returns "OK"
pyautogui.confirm('Asks OK or Cancel') # returns "OK" or "Cancel"
pyautogui.prompt('Asks for a string from user') # returns string or None
pyautogui.password('Enter password') # returns string or None
# Method signatures:
# alert(text='', title='', button='OK')
# confirm(text='', title='', buttons=['OK', 'Cancel'])
# prompt(text='', title='' , default='')
# password(text='', title='', default='', mask='*')
- Read more at the PyAutoGUI documentation.
- Read more about the Message Box Functions.
Using tkinter (cross-platform)
Tkinter is a cross-platform library. It comes with most Python installations and it works on Windows, Mac, and Linux.
If you want to learn more about the larget tkinter framework, check out my tutorial on GUI Programming with Python tkinter.
You may need to install the tkinter packages in Ubuntu with:
sudo apt install python-tk python3-tk
# sudo apt install python-tk python3-tk
from tkinter import messagebox # Python 3
# import tkMessageBox as messagebox # Python 2
messagebox.showinfo("Title", "message")
Here are a list of messagebox types.
They all accept parameters in the format of:
messagebox.function(title, message [, options])
messagebox.showinfo()
messagebox.showwarning()
messagebox.showerror()
messagebox.askquestion()
messagebox.askokcancel()
messagebox.askyesno()
messagebox.askretrycancel()
Read more about the tkinter messagebox.
Using ctypes (Windows only)
This method uses only the Python standard library and the Windows API. It requires no extra Python libraries. This solution came from a Stack Overflow answer.
import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW # MessageBoxA in Python2
MessageBox(None, 'Message\nfor\nthe\nuser', 'Window title', 0)
Using pywin32 (Windows only)
This solution uses pywin32
to interact with the Windows API.
You can view the source code for pywin32 on GitHub.
First install the pywin32
Python package dependency:
python -m pip install pywin32
Here is an example of how to use it:
# python -m pip install pywin32
import win32ui
import win32con # constants
# A simple notification
win32ui.MessageBox("Message", "Title")
# A prompt for Yes/No/Cancel
response = win32ui.MessageBox("Message", "Title", win32con.MB_YESNOCANCEL)
if response == win32con.IDYES:
win32ui.MessageBox("You pressed yes")
elif response == win32con.IDNO:
win32ui.MessageBox("You pressed no")
elif response == win32con.IDCANCEL:
win32ui.MessageBox("You pressed cancel")
Check out the full list of
constants in win32con.
This is where you can find what the potential options for button presses
are (ID*
) and what the different MessageBox types there are (MB_*
).
# Potential dialog responses
IDOK
IDCANCEL
IDABORT
IDRETRY
IDIGNORE
IDYES
IDNO
IDCLOSE
IDHELP
# MessageBox types
MB_OK
MB_OKCANCEL
MB_ABORTRETRYIGNORE
MB_YESNOCANCEL
MB_YESNO
MB_RETRYCANCEL
MB_ICONASTERISK
MB_ICONERROR
It's not the easiest to remember and work with because of all the Windows API interaction, but it's a decent option.
Conclusion
You should know have a solid grasp on creating dialog boxes to prompt a user for a question or to alert them of some action.