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
Answer from Joran Beasley on Stack Overflowarcpy - ArcGIS python script freezes when using ctypes MessageBox - Geographic Information Systems Stack Exchange
Python How to keep MessageboxW on top of all other windows? - Stack Overflow
python - How to specify what actually happens when Yes/No is clicked with ctypes MessageBoxW? - Stack Overflow
ctypes - How to bring python message box in front? - Stack Overflow
I'm new to python3, and trying to learn about gui and popup messagebox
import ctypes a = 1 b = 2 result = a+b print(result) # work great MessageBox = ctypes.windll.user32.MessageBoxW MessageBox(None, result, 'Window title', 0) # doesn't work
Any idea on how can I display valiable in ctypes messagebox?
Something like this with proper ctypes wrapping:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import ctypes
from ctypes.wintypes import HWND, LPWSTR, UINT
_user32 = ctypes.WinDLL('user32', use_last_error=True)
_MessageBoxW = _user32.MessageBoxW
_MessageBoxW.restype = UINT # default return type is c_int, this is not required
_MessageBoxW.argtypes = (HWND, LPWSTR, LPWSTR, UINT)
MB_OK = 0
MB_OKCANCEL = 1
MB_YESNOCANCEL = 3
MB_YESNO = 4
IDOK = 1
IDCANCEL = 2
IDABORT = 3
IDYES = 6
IDNO = 7
def MessageBoxW(hwnd, text, caption, utype):
result = _MessageBoxW(hwnd, text, caption, utype)
if not result:
raise ctypes.WinError(ctypes.get_last_error())
return result
def main():
try:
result = MessageBoxW(None, "text", "caption", MB_YESNOCANCEL)
if result == IDYES:
print("user pressed ok")
elif result == IDNO:
print("user pressed no")
elif result == IDCANCEL:
print("user pressed cancel")
else:
print("unknown return code")
except WindowsError as win_err:
print("An error occurred:\n{}".format(win_err))
if __name__ == "__main__":
main()
See the documentation for MessageBox for the various value of the utype argument.
Quoting official docs:
Return value
Type: int
If a message box has a Cancel button, the function returns the IDCANCEL value if either the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect. If the function fails, the return value is zero. To get extended error information, call GetLastError. If the function succeeds, the return value is one of the following menu-item values
You may checked listed values under official docs link.
Sample code would be something like:
def addnewunit(title, text, style):
ret_val = ctypes.windll.user32.MessageBoxW(0, text, title, style)
if ret_val == 0:
raise Exception('Oops')
elif ret_val == 1:
print "OK Clicked"
... # additional conditional checks of ret_val may go here
If you want a simple solution, use the PyMsgBox module. It uses Python's built-in tkinter library to create message boxes, including ones that let the user type a response. Install it with pip install pymsgbox.
The documentation is here: https://pymsgbox.readthedocs.org/
The code you want is:
>>> import pymsgbox
>>> returnValue = pymsgbox.prompt('Message box!', 'Title')
Message box is for messages only. What you need is QDialog. You can create it in QtDesigner(I have login dialog created this way, with 2 QLineEdit for username and pass, 2 buttons in QDialogButtonBox and QCombobox for language choose). You'll get .ui file, which you'll need to convert into .py this way in cmd:
pyuic4 -x YourLoginDialogWindow.ui -o YourLoginDialogWindow.py
import created YourLoginDialogWindow.py and you can use it and implement any method you need:
import YourLoginDialogWindow
class YourLoginDialog(QtGui.QDialog):
def __init__(self, parent = None):
super(YourLoginDialog, self).__init__(parent)
self.__ui = YourLoginDialogWindow.Ui_Dialog()
self.__ui.setupUi(self)
...
self.__ui.buttonBox.accepted.connect(self.CheckUserCredentials)
self.__ui.buttonBox.rejected.connect(self.reject)
def GetUsername(self):
return self.__ui.usernameLineEdit.text()
def GetUserPass(self):
return self.__ui.passwordLineEdit.text()
def CheckUserCredentials(self):
#check if user and pass are ok here
#you can use self.GetUsername() and self.GetUserPass() to get them
if THEY_ARE_OK :
self.accept()# this will close dialog and open YourMainProgram in main
else:# message box to inform user that username or password are incorect
QtGui.QMessageBox.about(self,'MESSAGE_APPLICATION_TITLE_STR', 'MESSAGE_WRONG_USERNAM_OR_PASSWORD_STR')
in your __main__ first create login dialog and then your main window...
if __name__ == "__main__":
qtApp = QtGui.QApplication(sys.argv)
loginDlg = YourLoginDialog.YourLoginDialog()
if (not loginDlg.exec_()):
sys.exit(-1)
theApp = YourMainProgram.YourMainProgram( loginDlg.GetUsername(), loginDlg.GetPassword())
qtApp.setActiveWindow(theApp)
theApp.show()
sys.exit(qtApp.exec_())
Edit: Resolved.
Is there an issue with MessageBoxW from ctypes?
If I set the button style to 0 then it creates the message box. If set to any of the other valid number 1 to 6 no box appears and the return code is 0?
# Python3
import ctypes
message = "This is a ctype MessageBoxW"
title = "Hello"
style = 1
result = ctypes.windll.user32.MessageBoxW(style, message, title)
print("Returned Code =", result)
input()
Or is something wrong with my Python setup?