How can I create a simple message box in Python? How can I create a simple message box in Python? tkinter tkinter

How can I create a simple message box in Python?


You could use an import and single line code like this:

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

Or define a function (Mbox) like so:

import ctypes  # An included library with Python install.def Mbox(title, text, style):    return ctypes.windll.user32.MessageBoxW(0, text, title, style)Mbox('Your title', 'Your text', 1)

Note the styles are as follows:

##  Styles:##  0 : OK##  1 : OK | Cancel##  2 : Abort | Retry | Ignore##  3 : Yes | No | Cancel##  4 : Yes | No##  5 : Retry | Cancel ##  6 : Cancel | Try Again | Continue

Have fun!

Note: edited to use MessageBoxW instead of MessageBoxA


Have you looked at easygui?

import easyguieasygui.msgbox("This is a message!", title="simple gui")


Also you can position the other window before withdrawing it so that you position your message

#!/usr/bin/env pythonfrom Tkinter import *import tkMessageBoxwindow = Tk()window.wm_withdraw()#message at x:200,y:200window.geometry("1x1+200+200")#remember its .geometry("WidthxHeight(+or-)X(+or-)Y")tkMessageBox.showerror(title="error",message="Error Message",parent=window)#centre screen messagewindow.geometry("1x1+"+str(window.winfo_screenwidth()/2)+"+"+str(window.winfo_screenheight()/2))tkMessageBox.showinfo(title="Greetings", message="Hello World!")