Python Central HubSign in

Automation and Scripting / Phase 5: GUI & System Control

Creating Simple Message Box Alerts

Show quick GUI alerts and prompts using pyautogui.

Alert

Python
import pyautogui
 
pyautogui.alert("Automation completed", title="Done")

Confirm

Python
import pyautogui
 
ans = pyautogui.confirm("Proceed?", buttons=["Yes", "No"])
print(ans)

What message boxes are for

A script that runs on your own desktop sometimes needs to talk to you: tell you a long job has finished, ask whether to continue, or ask for a value. pyautogui includes four small functions for this, based on the Tk toolkit that ships with Python. They open a native dialog, wait, and return what you chose.

FunctionShowsReturns
alert(text, title, button)a message and one buttonthe button text
confirm(text, title, buttons)a message and several buttonsthe text of the button pressed
prompt(text, title, default)a text fieldthe string typed, or None if cancelled
password(text, title, mask="*")a text field that hides typingthe string typed, or None

The call blocks: the script stops on that line until you press a button. That is the point, but it also means an unattended script that shows a dialog will wait forever. Use dialogs only when a person is present.

A confirmation before a risky step

confirm is a good safety gate, because the script continues only when you press the right button. Always handle None from prompt and password, and treat closing the window as "no".

Python
import pyautogui
 
 
def ask_to_continue(count: int) -> bool:
    answer = pyautogui.confirm(
        f"About to process {count} files. Continue?",
        title="Confirm",
        buttons=["Continue", "Cancel"],
    )
    return answer == "Continue"
 
 
def main() -> None:
    name = pyautogui.prompt("Folder to process:", title="Input", default="demo")
    if not name:
        pyautogui.alert("Cancelled, nothing was changed.", title="Stopped")
        return
 
    if not ask_to_continue(12):
        return
 
    # ... do the work here ...
    pyautogui.alert("Automation completed", title="Done")
 
 
if __name__ == "__main__":
    main()

Closing the confirm window returns None, which does not equal "Continue", so closing safely counts as cancelling. Comparing to the one button you want to act on is safer than comparing to the cancel button.

Alternatives

  • For a message that should not block the script, use a desktop notification library such as plyer (from plyer import notification), or send yourself a Telegram or Slack message.
  • For a full form, use tkinter directly.
  • On a server, or for scripts run by a scheduler, dialogs do not work at all because there is no display. Use logging and the messaging lessons instead.

Common mistakes

  • Showing a dialog in a scheduled job. It waits for a click that never comes.
  • Not handling None. Cancel or close returns None, and None.strip() raises AttributeError.
  • Putting passwords in a normal prompt. Use password, and never print the value.
  • Missing Tk. Some Linux installs need the python3-tk package.
  • Confusing buttons. confirm returns the button label you supplied, not a number or a boolean.

Check your understanding

quizCheck yourself
  1. What does pyautogui.prompt return when the user presses Cancel?

    Show answer

    B — None — Cancel returns None, so check for it before using the value as a string.

  2. Why are dialogs a poor choice for a script run by Task Scheduler or cron?

    Show answer

    A — They wait for a person to click, and there is often no display, so the job never finishes — A dialog blocks until someone responds. Unattended jobs should log or send a message instead.

  3. In a confirm dialog with buttons Continue and Cancel, how should you decide to proceed?

    Show answer

    B — Proceed only if the answer equals Continue — Closing the window returns None. Requiring an explicit Continue means any other outcome is treated as no.

Was this page helpful?

Let us know how we did