45

Is there a messagebox class where I can just display a simple message box without a huge GUI library or any library upon program success or failure. (My script only does 1 thing).

Also, I only need it to run on Windows.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Pwnna
  • 9,178
  • 21
  • 65
  • 91

6 Answers6

87

You can use the ctypes library, which comes installed with Python:

import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(None, 'Hello', 'Window title', 0)

Above code is for Python 3.x. For Python 2.x, use MessageBoxA instead of MessageBoxW as Python 2 uses non-unicode strings by default.

tav
  • 587
  • 6
  • 9
interjay
  • 107,303
  • 21
  • 270
  • 254
  • 1
    Yay for ctypes. That's what I was going to suggest but you beat me :-) – Chris Morgan Dec 19 '10 at 23:25
  • 9
    You can use `MessageBoxW` in Python2, too: `MessageBoxW(0, u'Hello', u'Window title', 0)`. – Glenn Maynard Dec 19 '10 at 23:31
  • 5
    It's worth mentioning here that ctypes is a module for calling external libraries (in this case the Windows user32 api), and that the solution presented is therefore Windows only (although ctypes itself is not). – Peter Gibson Dec 20 '10 at 00:19
  • Glad I stumbled across this. This should definitely be in more places. – mowwwalker May 24 '12 at 07:02
  • tkinter, answered below, works fine too, it also gives you nice icons. But you'll need to import tkinter to hide the tkinter's main window `import tkinter root = tkinter.Tk() root.withdraw()` see [link](https://pythonspot.com/en/tk-message-box/) for more details – Weihui Guo Oct 18 '17 at 17:34
  • You can get icons by passing `win32con.MB_ICONINFORMATION`, `win32con.MB_ICONWARNING`, `win32con.MB_USERICON` for the style, if you use win32con, win32gui instead of ctypes, or else you could directly pass the icon style number as the last argument like 48 for warning, 64 for information, etc. You can [get all the icon names and values here](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox) or import win32con in the python shell and enter `[x for x in dir(win32con) if x.startswith('MB_')]` – Vinayak Mar 02 '20 at 18:40
18

There are also a couple prototyped in the default libraries without using ctypes.

Simple message box:

import win32ui
win32ui.MessageBox("Message", "Title")

Other Options

if win32ui.MessageBox("Message", "Title", win32con.MB_YESNOCANCEL) == win32con.IDYES:
    win32ui.MessageBox("You pressed 'Yes'")

There's also a roughly equivalent one in win32gui and another in win32api. Docs for all appear to be in C:\Python{nn}\Lib\site-packages\PyWin32.chm

Wade Hatler
  • 1,785
  • 20
  • 18
  • 5
    `win32ui` isn't a default library. It's part of `PythonWin` which is distributed with `pywin32`. Packages installed in `site-packages` are all non-default. – Peter Wood Dec 07 '15 at 09:02
6

The PyMsgBox module uses Python's tkinter, so it doesn't depend on any other third-party modules. You can install it with pip install pymsgbox. It works on Windows, macOS, and Linux.

The function names are similar to JavaScript's alert(), confirm(), and prompt() functions:

>>> import pymsgbox
>>> pymsgbox.alert('This is an alert!')
>>> user_response = pymsgbox.prompt('What is your favorite color?')
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
2

A quick and dirty way is to call OS and use "zenity" command (subprocess module should be included by default in any python distribution, zenity is also present in all major linux). Try this short example script, it works in my Ubuntu 14.04.

import subprocess as SP
# call an OS subprocess $ zenity --entry --text "some text"
# (this will ask OS to open a window with the dialog)
res=SP.Popen(['zenity','--entry','--text',
'please write some text'], stdout=SP.PIPE)
# get the user input string back
usertext=str(res.communicate()[0][:-1])
# adjust user input string 
text=usertext[2:-1]
print("I got this text from the user: %s"%text)

See the zenity --help for more complex dialogs

2

You can also use the messagebox class from tkinter: from tkinter import messagebox unless tkinter is that huge GUI you want to avoid. Usage is simple, ie: messagebox.FunctionName(title, message [, options]) with FuntionName in (showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel).

Kurt K
  • 21
  • 1
1

This one with tkinter.

from tkinter import * #required.
from tkinter import messagebox #for messagebox.

App = Tk() #required.
App.withdraw() #for hide window.

print("Message Box in Console")
messagebox.showinfo("Notification", "Hello World!") #msgbox

App.mainloop() #required.
ForceVII
  • 355
  • 2
  • 16