Python消息框没有巨大的库依赖

Pwn*_*nna 35 python windows windows-7

是否有一个messagebox类,我可以在程序成功或失败时显示一个没有庞大的GUI库或任何库的简单消息框.(我的脚本只做一件事).

另外,我只需要它在Windows上运行.

int*_*jay 61

您可以使用随Python一起安装的ctypes库:

import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(None, 'Hello', 'Window title', 0)
Run Code Online (Sandbox Code Playgroud)

上面的代码适用于Python 3.x. 对于Python 2.x,使用MessageBoxA而不是MessageBoxWPython 2默认情况下使用非unicode字符串.

  • 您也可以在Python2中使用`MessageBoxW`:`MessageBoxW(0,u'Hello',u'Window title',0)`. (8认同)
  • 值得一提的是,ctypes是一个用于调用外部库的模块(在本例中是Windows user32 api),因此所提供的解决方案仅适用于Windows(尽管ctypes本身不是). (3认同)

Wad*_*ler 11

在没有使用ctypes的情况下,默认库中还有一对原型.

简单的消息框:

import win32ui
win32ui.MessageBox("Message", "Title")
Run Code Online (Sandbox Code Playgroud)

其他选择

if win32ui.MessageBox("Message", "Title", win32con.MB_YESNOCANCEL) == win32con.IDYES:
    win32ui.MessageBox("You pressed 'Yes'")
Run Code Online (Sandbox Code Playgroud)

在win32gui中还有一个大致相当的一个,在win32api中也是另一个.所有人的文件似乎都在C:\Python{nn}\Lib\site-packages\PyWin32.chm

  • `win32ui`不是默认库.它是`PythonWin`的一部分,它与`pywin32`一起发布.安装在`site-packages`中的软件包都是非默认的. (3认同)

Al *_*art 5

PyMsgBox 模块使用 Python 的 tkinter,因此它不依赖于任何其他第三方模块。您可以使用pip install pymsgbox.

函数名类似于JavaScript的alert()confirm()prompt()功能:

>>> import pymsgbox
>>> pymsgbox.alert('This is an alert!')
>>> user_response = pymsgbox.prompt('What is your favorite color?')
Run Code Online (Sandbox Code Playgroud)