简体   繁体   English

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

[英]Python Message Box Without huge library dependency

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.是否有一个消息框类,我可以在程序成功或失败时显示一个简单的消息框,而无需庞大的 GUI 库或任何库。 (My script only does 1 thing). (我的脚本只做一件事)。

Also, I only need it to run on Windows.另外,我只需要它在 Windows 上运行。

You can use the ctypes library, 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.以上代码适用于 Python 3.x。 For Python 2.x, use MessageBoxA instead of MessageBoxW as Python 2 uses non-unicode strings by default.对于 Python 2.x,使用MessageBoxA而不是MessageBoxW因为 Python 2 默认使用非 unicode 字符串。

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.在 win32gui 和 win32api 中也有大致相同的一个。 Docs for all appear to be in C:\\Python{nn}\\Lib\\site-packages\\PyWin32.chm所有文档似乎都在C:\\Python{nn}\\Lib\\site-packages\\PyWin32.chm

The PyMsgBox module uses Python's tkinter, so it doesn't depend on any other third-party modules. PyMsgBox 模块使用 Python 的 tkinter,因此它不依赖于任何其他第三方模块。 You can install it with pip install pymsgbox .您可以使用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.prompt('What is your favorite color?')

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).一种快速而肮脏的方法是调用操作系统并使用“zenity”命令(子进程模块应默认包含在任何 python 发行版中,zenity 也存在于所有主要的 linux 中)。 Try this short example script, it works in my Ubuntu 14.04.试试这个简短的示例脚本,它适用于我的 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

You can also use the messagebox class from tkinter: from tkinter import messagebox unless tkinter is that huge GUI you want to avoid.您还可以使用 tkinter 的 messagebox 类: from tkinter import messagebox除非 tkinter 是您想要避免的巨大 GUI。 Usage is simple, ie: messagebox.FunctionName(title, message [, options]) with FuntionName in (showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel).用法很简单,即: messagebox.FunctionName(title, message [, options])和FuntionName in (showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel)。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM