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!
2 Answers
You could do
import ctypes
mymessage = 'A message'
title = 'Popup window'
ctypes.windll.user32.MessageBoxW(0, mymessage, title, 0)
ctypes is part of the standard library, so this works on Windows without the need to install any packages.
3 Comments
MessageBoxA shows the complete strings. Must be something to do with string encodings.ctypes.windll.user32.MessageBoxW(0, mymessage, title, 0) for full messageA most simple way I could think of (not necessarily elegant) would be creating a .vbs file and calling MsgBox from there
MsgBox "Window not found!", 65, "Information"
Then call that file from Python using
>>> import os
>>> os.system("call popupmessage.vbs")
However, this is not cross-platform, but I assume you are using Windows. Also keep in mind the directory in the os.system call is relative to your working directory unless you specify the full (or absolute) directory.
You should refer to https://www.tutorialspoint.com/vbscript/vbscript_dialog_boxes.htm on how to use MsgBox