tags:

views:

51

answers:

5

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.

A: 

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.

Zonda333
That works, thanks! Not so great that I have to install another library, though. And it is based on Tkinter. I wanted something that would call the windows api directly.
Scribble Master
+1  A: 

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.

Constantin
+2  A: 

@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.

Chris
I'll keep this one in mind.
Scribble Master
Almost correct :) In 3.x `ctypes.windll.user32.MessageBoxW(0, "Error", "Error", 0)` would have worked as expected, and `u"Error"` would not compile at all.
Constantin
Ah, good to know. It's gonna be a major headache when I'm forced to move to py3k!
Chris
I'm using 2.5 for compatibility reasons, so Chris's is what I needed.Moving to 3.0 really is going to be very difficult!
Scribble Master
@Chris, @Scribble Master, fortunately official 2to3 conversion tool removes much of that headache.
Constantin
+1  A: 

You can get a one-liner using tkinter.

import tkMessageBox

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

Here is some documentation for pop-up dialogs.

PreludeAndFugue
Ah, I didn't know that, thanks!I do prefer the direct win32 call, though
Scribble Master
+1  A: 

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

asd32