Simple Windows Popup Window using Python

I am trying to find the easiest (with shortest code) way of displaying a popup dialog window in Windows in my non-GUI Python 3.7 program. Basically, it tries to find and grab control of a window with a particular name. If it finds the window, it does its thing and all is good. If it doesn’t find the window I’d like a wee popup message saying “Window not found”. I know I can do this using PyQt5 or similar, but it is an awful lot of code just display a dialog!

Answer

You could do

import ctypes

mymessage = 'A message'
title = 'Popup window'
ctypes.windll.user32.MessageBoxA(0, mymessage, title, 0)

ctypes is part of the standard library, so this works on Windows without the need to install any packages.

Leave a Reply

Your email address will not be published. Required fields are marked *