Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python消息框没有巨大的库依赖性_Python_Windows_User Interface_Windows 7 - Fatal编程技术网

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

Python消息框没有巨大的库依赖性,python,windows,user-interface,windows-7,Python,Windows,User Interface,Windows 7,是否有一个messagebox类,在该类中,我可以在程序成功或失败时只显示一个简单的消息框,而不显示巨大的GUI库或任何库。(我的脚本只做一件事) 另外,我只需要它在Windows上运行。您可以使用随Python一起安装的库: import ctypes MessageBox = ctypes.windll.user32.MessageBoxW MessageBox(None, 'Hello', 'Window title', 0) 以上代码是针对Python3.x的。对于Python2.x,

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

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

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

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

以上代码是针对Python3.x的。对于Python2.x,使用
MessageBoxA
而不是
MessageBoxW
,因为Python2默认使用非unicode字符串。

默认库中还有一些原型,没有使用ctypes

简单信息框:

import win32ui
win32ui.MessageBox("Message", "Title")
其他选择

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

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

一种快速而肮脏的方法是调用操作系统并使用“zenity”命令(默认情况下,任何Python发行版中都应包含子流程模块,所有主要linux中也都存在zenity)。试试这个简短的示例脚本,它在我的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)

有关更复杂的对话框,请参见zenity--help。您还可以使用tkinter中的messagebox类:
来自tkinter导入消息框
除非tkinter是您想要避免的巨大GUI。 用法很简单,即:
messagebox.FunctionName(标题、消息[、选项])

FunctionName处于(showinfo、showwarning、showerror、askquestion、askokcancel、askyesno、askretrycancel)

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?')
这是和特金特在一起的

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.

这里有两个独立的问题。你应该把它们分成不同的问题。好的。另一个问题是关于ctypes的问题。这就是我要建议的,但你打败了我:-)你也可以在Python2中使用
MessageBoxW
MessageBoxW(0,u'Hello',u'windowtitle',0)
。这里值得一提的是,ctypes是一个调用外部库的模块(在本例中是WindowsUser32API),因此提供的解决方案仅适用于Windows(虽然ctypes本身不是)。很高兴我偶然发现了这一点。这肯定会出现在更多的地方。下面回答的tkinter也很好,它还提供了很好的图标。但是您需要导入tkinter来隐藏tkinter的主窗口
import tkinter root=tkinter.Tk()root.draw()
有关详细信息,请参见
win32ui
不是默认库。它是
PythonWin
的一部分,与
pywin32
一起分发。安装在
站点包中的包都是非默认包。