0

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 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

MessageBoxA only shows the first letter of the title and first letter of the caption for me, so I changed it to MessageBoxW to view the full strings: ctypes.windll.user32.MessageBoxW(0, mymessage, title, 0)
Ok, that's interesting. For me, MessageBoxA shows the complete strings. Must be something to do with string encodings.
Use ctypes.windll.user32.MessageBoxW(0, mymessage, title, 0) for full message
0

A 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

1 Comment

This works perfectly. To only display an OK button, I changed the MsgBox command to: MsgBox "Window not found!", , "Information"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.