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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4485610/
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-08-18 15:58:25  来源:igfitidea点击:

Python Message Box Without huge library dependency

pythonwindowswindows-7

提问by Pwnna

Is there a messagebox class where I can just display a simple message box without a huge GUI library or any library upon program success or failure. (My script only does 1 thing).

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

Also, I only need it to run on Windows.

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

采纳答案by interjay

You can use the ctypeslibrary, which comes installed with Python:

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

import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(None, 'Hello', 'Window title', 0)

Above code is for Python 3.x. For Python 2.x, use MessageBoxAinstead of MessageBoxWas Python 2 uses non-unicode strings by default.

以上代码适用于 Python 3.x。对于 Python 2.x,使用MessageBoxA而不是MessageBoxW因为 Python 2 默认使用非 unicode 字符串。

回答by Wade Hatler

There are also a couple prototyped in the default libraries without using ctypes.

在不使用 ctypes 的情况下,默认库中还有一些原型。

Simple message box:

简单的消息框:

import win32ui
win32ui.MessageBox("Message", "Title")

Other Options

其他选项

if win32ui.MessageBox("Message", "Title", win32con.MB_YESNOCANCEL) == win32con.IDYES:
    win32ui.MessageBox("You pressed 'Yes'")

There's also a roughly equivalent one in win32gui and another in win32api. Docs for all appear to be in C:\Python{nn}\Lib\site-packages\PyWin32.chm

在 win32gui 和 win32api 中也有大致相同的一个。所有人的文档似乎都在C:\Python{nn}\Lib\site-packages\PyWin32.chm

回答by Mario Rossi

A quick and dirty way is to call OS and use "zenity" command (subprocess module should be included by default in any python distribution, zenity is also present in all major linux). Try this short example script, it works in my Ubuntu 14.04.

一种快速而肮脏的方法是调用操作系统并使用“zenity”命令(子进程模块应该默认包含在任何 python 发行版中,zenity 也存在于所有主要的 linux 中)。试试这个简短的示例脚本,它适用于我的 Ubuntu 14.04。

import subprocess as SP
# call an OS subprocess $ zenity --entry --text "some text"
# (this will ask OS to open a window with the dialog)
res=SP.Popen(['zenity','--entry','--text',
'please write some text'], stdout=SP.PIPE)
# get the user input string back
usertext=str(res.communicate()[0][:-1])
# adjust user input string 
text=usertext[2:-1]
print("I got this text from the user: %s"%text)

See the zenity --help for more complex dialogs

有关更复杂的对话框,请参阅 zenity --help

回答by Kurt K

You can also use the messagebox class from tkinter: from tkinter import messageboxunless tkinter is that huge GUI you want to avoid. Usage is simple, ie: messagebox.FunctionName(title, message [, options])with FuntionName in (showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel).

你也可以使用 tkinter 的 messagebox 类: from tkinter import messagebox除非 tkinter 是你想要避免的那个巨大的 GUI。用法很简单,即: messagebox.FunctionName(title, message [, options])在(showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel) 中使用FuntionName。

回答by Al Sweigart

The PyMsgBox module uses Python's tkinter, so it doesn't depend on any other third-party modules. You can install it with pip install pymsgbox.

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

The function names are similar to JavaScript's alert(), confirm(), and prompt()functions:

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

>>> import pymsgbox
>>> pymsgbox.alert('This is an alert!')
>>> user_response = pymsgbox('What is your favorite color?')

回答by ForceVII

This one with tkinter.

这一个与 tkinter。

from tkinter import * #required.
from tkinter import messagebox #for messagebox.

App = Tk() #required.
App.withdraw() #for hide window.

print("Message Box in Console")
messagebox.showinfo("Notification", "Hello World!") #msgbox

App.mainloop() #required.