Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Message Box Without huge library dependency

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.

like image 984
Pwnna Avatar asked Dec 19 '10 22:12

Pwnna


3 Answers

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.

like image 190
interjay Avatar answered Nov 03 '22 01:11

interjay


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

like image 24
Wade Hatler Avatar answered Nov 03 '22 01:11

Wade Hatler


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.

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?')
like image 5
Al Sweigart Avatar answered Nov 03 '22 03:11

Al Sweigart