16

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.

Community
  • 1
  • 1
MrBubbles
  • 405
  • 2
  • 7
  • 19

1 Answers1

20

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

Nuno André
  • 4,739
  • 1
  • 33
  • 46
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • doh! thinking wayyyyyyy to hard about this. lol Thanks so much Joran – MrBubbles Dec 02 '14 at 19:54
  • 1
    Joran, can you tell me how you deciphered what those hex values were? The hex values you are using seem to be an abbreviated form of what microsoft shows on their site. I originally tried passing in the hex values microsoft indicates as they were, but they never worked. Yours do work, I just don't know how you got to 0x30 from 0x00000030L, which is the value microsoft's documentation indicates for the exclamation icon – MrBubbles Dec 03 '14 at 00:55
  • 2
    zeros on the left are not part of a number eg in decimal 97 is the same as 00097 ... I could have just as easily written 0x00000030 ... L just means `long` as in the datatype (any number that starts with `0x` is just a hex number) – Joran Beasley Dec 03 '14 at 01:05
  • What is the reasoning that OR is what groups them together? – Mojimi Jun 27 '17 at 18:35
  • I dont understand your question? thats how you always combine integer flags in every example of anything ive ever seen ... I guess what informs it is my knowledge that when some one says "combine these integers" I know they mean `or` them together – Joran Beasley Jun 27 '17 at 19:39
  • @JoranBeasley Well if you say so, I just never even heard of "combining integers" – Mojimi Jun 29 '17 at 19:34
  • its very common in C/C++ ... much less so in python – Joran Beasley Jun 29 '17 at 19:40
  • 1
    I would also use `MessageBoxW()` to deal correctly with unicode. – Tiger-222 Jun 04 '19 at 15:17
  • Indeed, I needed to use `MessageBoxW()` to show the whole strings and not only the first letter. Windows10, python3.8 – Zaero Divide Jul 27 '22 at 12:35