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
2010-07-29 18:16:28
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
2010-07-29 18:25:24
+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
2010-07-29 18:23:39
+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
2010-07-29 18:25:33
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
2010-07-29 18:32:56
Ah, good to know. It's gonna be a major headache when I'm forced to move to py3k!
Chris
2010-07-29 18:34:41
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
2010-07-29 18:38:26
@Chris, @Scribble Master, fortunately official 2to3 conversion tool removes much of that headache.
Constantin
2010-07-29 20:52:27
+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
2010-07-29 18:32:41
Ah, I didn't know that, thanks!I do prefer the direct win32 call, though
Scribble Master
2010-07-29 18:40:19