如何在 Python 中创建一个简单的消息框?
我正在寻找与 JavaScript 中的 alert()
相同的效果.
I'm looking for the same effect as alert()
in JavaScript.
今天下午我使用 Twisted.web 编写了一个简单的基于 Web 的解释器.你基本上是通过一个表单提交一段 Python 代码,然后客户端来抓取它并执行它.我希望能够制作一个简单的弹出消息,而不必每次都重新编写一大堆样板 wxPython 或 TkInter 代码(因为代码通过表单提交然后消失).
I wrote a simple web-based interpreter this afternoon using Twisted.web. You basically submit a block of Python code through a form, and the client comes and grabs it and executes it. I want to be able to make a simple popup message, without having to re-write a whole bunch of boilerplate wxPython or TkInter code every time (since the code gets submitted through a form and then disappears).
我试过 tkMessageBox:
I've tried tkMessageBox:
import tkMessageBox
tkMessageBox.showinfo(title="Greetings", message="Hello World!")
但这会在后台打开另一个带有 tk 图标的窗口.我不要这个.我正在寻找一些简单的 wxPython 代码,但它总是需要设置一个类并进入一个应用程序循环等.在 Python 中没有简单的、无捕获的方法来制作消息框吗?
but this opens another window in the background with a tk icon. I don't want this. I was looking for some simple wxPython code but it always required setting up a class and entering an app loop etc. Is there no simple, catch-free way of making a message box in Python?
您可以使用这样的导入和单行代码:
You could use an import and single line code like this:
import ctypes # An included library with Python install.
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)
或者像这样定义一个函数(Mbox):
Or define a function (Mbox) like so:
import ctypes # An included library with Python install.
def Mbox(title, text, style):
return ctypes.windll.user32.MessageBoxW(0, text, title, style)
Mbox('Your title', 'Your text', 1)
注意样式如下:
## Styles:
## 0 : OK
## 1 : OK | Cancel
## 2 : Abort | Retry | Ignore
## 3 : Yes | No | Cancel
## 4 : Yes | No
## 5 : Retry | Cancel
## 6 : Cancel | Try Again | Continue
玩得开心!
注意:编辑为使用 MessageBoxW
而不是 MessageBoxA
Note: edited to use MessageBoxW
instead of MessageBoxA