Scribble Master
Scribble Master

Reputation: 1130

How to throw an error window in Python in Windows

What's the easiest way to generate an error window for a Python script in Windows? Windows-specific answers are fine; please don't reply how to generate a custom Tk window.

Upvotes: 11

Views: 24798

Answers (5)

asd32
asd32

Reputation: 201

Check out the GUI section of the Python Wiki for info on message boxs

Upvotes: 2

Gary Kerr
Gary Kerr

Reputation: 14410

You can get a one-liner using tkinter.

import tkMessageBox

tkMessageBox.showerror('error title', 'error message')

Here is some documentation for pop-up dialogs.

Upvotes: 2

Constantin
Constantin

Reputation: 28154

If i recall correctly (don't have Windows box at the moment), the ctypes way is:

import ctypes
ctypes.windll.user32.MessageBoxW(None, u"Error", u"Error", 0)

ctypes is a standard module.

Note: For Python 3.x you don't need the u prefix.

Upvotes: 4

Chris
Chris

Reputation: 1483

@Constantin is almost correct, but his example will produce garbage text. Make sure that the text is unicode. I.e.,

ctypes.windll.user32.MessageBoxW(0, u"Error", u"Error", 0)

...and it'll work fine.

Upvotes: 13

John Howard
John Howard

Reputation: 64105

If you need a GUI error message, you could use EasyGui:

>>> import easygui as e
>>> e.msgbox("An error has occured! :(", "Error")

Otherwise a simple print("Error!") should suffice.

Upvotes: 3

Related Questions