Car*_*ers 98 python wxpython tkinter
我正在寻找与alert()JavaScript 相同的效果.
我今天下午用Twisted.web写了一个简单的基于网络的翻译.你基本上通过一个表单提交一个Python代码块,然后客户端来抓取它并执行它.我希望能够制作一个简单的弹出消息,而不必每次都重写一大堆样板wxPython或TkInter代码(因为代码通过表单提交然后消失).
我试过tkMessageBox:
import tkMessageBox
tkMessageBox.showinfo(title="Greetings", message="Hello World!")
Run Code Online (Sandbox Code Playgroud)
但这会在背景中打开另一个带有tk图标的窗口.我不想要这个.我正在寻找一些简单的wxPython代码,但它总是需要设置一个类并进入一个应用程序循环等.在Python中没有简单,无需捕获的方法来制作消息框吗?
小智 217
你可以使用像这样的导入和单行代码:
import ctypes # An included library with Python install.
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)
Run Code Online (Sandbox Code Playgroud)
或者像这样定义一个函数(Mbox):
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)
Run Code Online (Sandbox Code Playgroud)
注意样式如下:
## Styles:
## 0 : OK
## 1 : OK | Cancel
## 2 : Abort | Retry | Ignore
## 3 : Yes | No | Cancel
## 4 : Yes | No
## 5 : Retry | No
## 6 : Cancel | Try Again | Continue
Run Code Online (Sandbox Code Playgroud)
玩得开心!
注意:编辑使用MessageBoxW而不是MessageBoxA
Rya*_*rom 48
你看过easygui吗?
import easygui
easygui.msgbox("This is a message!", title="simple gui")
Run Code Online (Sandbox Code Playgroud)
小智 21
您也可以在退出之前定位另一个窗口,以便定位消息
#!/usr/bin/env python
from Tkinter import *
import tkMessageBox
window = Tk()
window.wm_withdraw()
#message at x:200,y:200
window.geometry("1x1+200+200")#remember its .geometry("WidthxHeight(+or-)X(+or-)Y")
tkMessageBox.showerror(title="error",message="Error Message",parent=window)
#centre screen message
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title="Greetings", message="Hello World!")
Run Code Online (Sandbox Code Playgroud)
Jot*_*taf 19
你提供的代码很好!您只需要在后台显式创建"其他窗口"并使用以下代码隐藏它:
import Tkinter
window = Tkinter.Tk()
window.wm_withdraw()
Run Code Online (Sandbox Code Playgroud)
就在你的留言箱之前.
在Windows中,您可以将ctypes与user32库一起使用:
from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)
MessageBox()
MessageBox(text="Spam, spam, spam")
MessageBox(flags=2, text="foo bar")
Run Code Online (Sandbox Code Playgroud)
在Mac上,python标准库有一个名为的模块EasyDialogs.在http://www.averdevelopment.com/python/EasyDialogs.html上还有一个(基于ctypes的)windows版本
如果它对您很重要:它使用原生对话框,并且不像已经提到的那样依赖于Tkinter easygui,但它可能没有那么多功能.
PyMsgBox模块就是这样做的.它具有遵循JavaScript命名约定的消息框函数:alert(),confirm(),prompt()和password()(这是prompt()但在您键入时使用*).这些函数调用将阻塞,直到用户单击"确定/取消"按钮.它是一个跨平台的纯Python模块,没有依赖关系.
安装时间: pip install PyMsgBox
样品用法:
>>> import pymsgbox
>>> pymsgbox.alert('This is an alert!', 'Title')
>>> response = pymsgbox.prompt('What is your name?')
Run Code Online (Sandbox Code Playgroud)
http://pymsgbox.readthedocs.org/en/latest/上的完整文档
import ctypes
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)
Run Code Online (Sandbox Code Playgroud)
可以更改最后一个数字(此处为1)以更改窗口样式(不仅是按钮!):
## Button styles:
# 0 : OK
# 1 : OK | Cancel
# 2 : Abort | Retry | Ignore
# 3 : Yes | No | Cancel
# 4 : Yes | No
# 5 : Retry | No
# 6 : Cancel | Try Again | Continue
## To also change icon, add these values to previous number
# 16 Stop-sign icon
# 32 Question-mark icon
# 48 Exclamation-point icon
# 64 Information-sign icon consisting of an 'i' in a circle
Run Code Online (Sandbox Code Playgroud)
例如,
ctypes.windll.user32.MessageBoxW(0, "That's an error", "Warning!", 16)
Run Code Online (Sandbox Code Playgroud)
会给这个:
您可以使用pyautogui或pymsgbox:
import pyautogui
pyautogui.alert("This is a message box",title="Hello World")
Run Code Online (Sandbox Code Playgroud)
使用pymsgbox与使用相同pyautogui:
import pymsgbox
pymsgbox.alert("This is a message box",title="Hello World")
Run Code Online (Sandbox Code Playgroud)