Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MessageBox with Icons using ctypes and windll

So, I'm looking for a way to create a simple Messagebox in Python using just the native libraries and came across several posts, but namely this one, leveraging ctypes to import the win32.dll and call its MessageboxA function.

import ctypes  # An included library with Python install.
ctypes.windll.user32.MessageBoxA(0, "Your text", "Your title", 1)

Pretty cool stuff, I think.

--- But ---

When I when to look at the documentation for MessageboxA on Microsoft's site, turns out this MessageboxA function can do a whole lot more. I just don't know how to properly pass the parameters in.

I'm trying to figure out the standard method for raising the messagebox with an icon in it, like the systemhand or warning icon beside the message. Microsoft's documentation indicates one should enter this into the uType parameter, which is the last one, but I haven't been able to make any progress here other than changing the messagebox's buttons.

like image 751
MrBubbles Avatar asked Dec 02 '14 19:12

MrBubbles


1 Answers

you just OR them together

import ctypes

# buttons
MB_OK = 0x0
MB_OKCXL = 0x01
MB_YESNOCXL = 0x03
MB_YESNO = 0x04
MB_HELP = 0x4000

# icons
ICON_EXCLAIM = 0x30
ICON_INFO = 0x40
ICON_STOP = 0x10

result = ctypes.windll.user32.MessageBoxA(0, "Your text?", "Your title", MB_HELP | MB_YESNO | ICON_STOP)

I got the hex values from the documentation you linked to

like image 140
Joran Beasley Avatar answered Nov 15 '22 10:11

Joran Beasley