如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:55:44  来源:igfitidea点击:

How to throw an error window in Python in Windows

pythonwindows

提问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

Check out the GUIsection of the Python Wiki for info on message boxs

查看Python Wiki的GUI部分以获取有关消息框的信息

回答by John Howard

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

如果您需要 GUI 错误消息,您可以使用EasyGui

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

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

否则一个简单的print("Error!")就足够了。

回答by Constantin

If i recall correctly (don't have Windows box at the moment), the ctypesway 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 uprefix.

注意:对于 Python 3.x,您不需要u前缀。