如何在 Windows 中的 Python 中抛出错误窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3365673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to throw an error window in Python in Windows
提问by Scribble Master
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.
在 Windows 中为 Python 脚本生成错误窗口的最简单方法是什么?特定于 Windows 的答案很好;请不要回复如何生成自定义 Tk 窗口。
采纳答案by Chris
@Constantin is almost correct, but his example will produce garbage text. Make sure that the text is unicode. I.e.,
@Constantin 几乎是正确的,但他的例子会产生垃圾文本。确保文本是 unicode。IE,
ctypes.windll.user32.MessageBoxW(0, u"Error", u"Error", 0)
...and it'll work fine.
...它会正常工作。
回答by Gary Kerr
You can get a one-liner using tkinter.
您可以使用 tkinter 获得单线。
import tkMessageBox
tkMessageBox.showerror('error title', 'error message')
Here is some documentation for pop-up dialogs.
这是弹出对话框的一些文档。
回答by asd32
回答by John Howard
回答by Constantin
If i recall correctly (don't have Windows box at the moment), the ctypes
way is:
如果我没记错的话(目前没有 Windows 框),ctypes
方法是:
import ctypes
ctypes.windll.user32.MessageBoxW(None, u"Error", u"Error", 0)
ctypesis a standard module.
ctypes是一个标准模块。
Note: For Python 3.x you don't need the u
prefix.
注意:对于 Python 3.x,您不需要u
前缀。