![]() |
怕老婆的柠檬 · gRPC python 教程(四) ...· 12 小时前 · |
![]() |
飘逸的登山鞋 · python 并发请求grpc ...· 12 小时前 · |
![]() |
耍酷的木瓜 · Python中的并发控制 - ...· 12 小时前 · |
![]() |
谦虚好学的紫菜 · 推荐开源项目:Zeep - Python ...· 10 小时前 · |
![]() |
礼貌的消防车 · C#/.net程序调用python - ...· 6 小时前 · |
![]() |
越狱的鼠标 · eslint - ...· 5 月前 · |
![]() |
旅行中的硬盘 · PAT考试_浙大pat一年可以考几次-CSDN博客· 6 月前 · |
![]() |
爱健身的鼠标垫 · Listings Code Style ...· 6 月前 · |
![]() |
坚韧的柑橘 · 龙帝软件库安卓版下载-龙帝软件库最新版下载v ...· 7 月前 · |
![]() |
侠义非凡的冰棍 · Audi Sugar Land - New ...· 7 月前 · |
我正在寻找与JavaScript中的
alert()
相同的效果。
今天下午我用Twisted.web写了一个简单的基于web的解释器。基本上,您通过表单提交一段Python代码,然后客户端来获取它并执行它。我希望能够制作一条简单的弹出消息,而不必每次都重写一大堆样板wxPython或TkInter代码(因为代码是通过表单提交的,然后就消失了)。
我试过tkMessageBox:
import tkMessageBox
tkMessageBox.showinfo(title="Greetings", message="Hello World!")
但这会在后台打开另一个带有tk图标的窗口。我不想这样。我正在寻找一些简单的wxPython代码,但它总是需要设置一个类和进入一个应用程序循环等。
你有没有看过 easygui
import easygui
easygui.msgbox("This is a message!", title="simple gui")
在Windows中,您可以使用 ctypes with user32 library
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")
在Mac上,python标准库有一个名为
EasyDialogs
的模块。在
http://www.averdevelopment.com/python/EasyDialogs.html
上还有一个(基于ctype的) windows版本
如果这对你很重要:它使用原生对话框,并不像前面提到的
easygui
那样依赖于Tkinter,但它可能没有那么多功能。
您提供的代码是正确的!你只需要显式地创建“后台的其他窗口”并隐藏它,使用下面的代码:
import Tkinter
window = Tkinter.Tk()
window.wm_withdraw()
就在你的信箱前。
您还可以在收回其他窗口之前对其进行定位,以便对消息进行定位
#!/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!")
您可以使用导入和单行代码,如下所示:
import ctypes # An included library with Python install.
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)
或者像这样定义一个函数(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)
请注意,样式如下:
## Styles:
## 0 : OK
## 1 : OK | Cancel
## 2 : Abort | Retry | Ignore
## 3 : Yes | No | Cancel
## 4 : Yes | No
## 5 : Retry | Cancel
## 6 : Cancel | Try Again | Continue
玩得开心!
注:编辑为使用
MessageBoxW
而不是
MessageBoxA
使用
from tkinter.messagebox import *
Message([master], title="[title]", message="[message]")
主窗口必须在创建之前创建。这是为Python3编写的,不是为wxPython编写的,而是为tkinter编写的。
import sys
from tkinter import *
def mhello():
return
mGui = Tk()
ment = StringVar()
mGui.geometry('450x450+500+300')
mGui.title('My youtube Tkinter')
mlabel = Label(mGui,text ='my label').pack()
mbutton = Button(mGui,text ='ok',command = mhello,fg = 'red',bg='blue').pack()
mEntry = entry().pack
PyMsgBox模块就是这样做的。它具有遵循JavaScript命名约定的消息框函数: alert()、confirm()、prompt()和password() (它是prompt(),但在键入时使用*)。这些函数调用会一直阻塞,直到用户单击OK/Cancel按钮。它是一个跨平台的纯Python模块,在tkinter之外没有依赖关系。
安装时使用:
pip install PyMsgBox
示例用法:
import pymsgbox
pymsgbox.alert('This is an alert!', 'Title')
response = pymsgbox.prompt('What is your name?')
有关完整文档,请访问 http://pymsgbox.readthedocs.org/en/latest/
这不是最好的,这是我仅使用tkinter的基本消息框。
#Python 3.4
from tkinter import messagebox as msg;
import tkinter as tk;
def MsgBox(title, text, style):
box = [
msg.showinfo, msg.showwarning, msg.showerror,
msg.askquestion, msg.askyesno, msg.askokcancel, msg.askretrycancel,
tk.Tk().withdraw(); #Hide Main Window.
if style in range(7):
return box[style](title, text);
if __name__ == '__main__':
Return = MsgBox(#Use Like This.
'Basic Error Exemple',
''.join( [
'The Basic Error Exemple a problem with test', '\n',
'and is unable to continue. The application must close.', '\n\n',
'Error code Test', '\n',
'Would you like visit http://wwww.basic-error-exemple.com/ for', '\n',
'help?',
print( Return );
Style | Type | Button | Return
------------------------------------------------------
0 Info Ok 'ok'
1 Warning Ok 'ok'
2 Error Ok 'ok'
3 Question Yes/No 'yes'/'no'
4 YesNo Yes/No True/False
5 OkCancel Ok/Cancel True/False
6 RetryCancal Retry/Cancel True/False
"""
查看我的python模块: pip install quickgui (需要wxPython,但不需要wxPython知识) https://pypi.python.org/pypi/quickgui
可以创建任意数量的输入,(ratio,checkbox,inputbox),在单个gui上自动排列它们。
import ctypes
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)
最后一个数字(这里是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
例如,
ctypes.windll.user32.MessageBoxW(0, "That's an error", "Warning!", 16)
将会给 this
最近的消息框版本是prompt_box模块。它有两个包: alert和message。Message使您可以更好地控制该框,但需要更长的时间才能键入。
警报代码示例:
import prompt_box
prompt_box.alert('Hello') #This will output a dialog box with title Neutrino and the
#text you inputted. The buttons will be Yes, No and Cancel
示例消息代码:
import prompt_box
prompt_box.message('Hello', 'Neutrino', 'You pressed yes', 'You pressed no', 'You
pressed cancel') #The first two are text and title, and the other three are what is
#printed when you press a certain button
您还可以在收回其他窗口之前对其进行定位,以便对消息进行定位
from tkinter import *
import tkinter.messagebox
window = Tk()
window.wm_withdraw()
# message at x:200,y:200
window.geometry("1x1+200+200") # remember its.geometry("WidthxHeight(+or-)X(+or-)Y")
tkinter.messagebox.showerror(title="error", message="Error Message", parent=window)
# center screen message
window.geometry(f"1x1+{round(window.winfo_screenwidth() / 2)}+{round(window.winfo_screenheight() / 2)}")
tkinter.messagebox.showinfo(title="Greetings", message="Hello World!")
请注意:这是Lewis Cowles的答案,只是Python 3ify,因为tkinter从python 2开始已经改变了。如果你想让你的代码是backwords可比较的,可以这样做:
try:
import tkinter
import tkinter.messagebox
except ModuleNotFoundError:
import Tkinter as tkinter
import tkMessageBox as tkinter.messagebox
您可以使用
pyautogui
或
pymsgbox
import pyautogui
pyautogui.alert("This is a message box",title="Hello World")
使用
pymsgbox
与使用
pyautogui
相同
import pymsgbox
pymsgbox.alert("This is a message box",title="Hello World")
我不得不在现有的程序中添加一个消息框。在这种情况下,大多数答案都过于复杂。对于Ubuntu 16.04 (Python 2.7.12)上的Linux以及Ubuntu 20.04的未来校对,以下是我的代码:
程序顶部
from __future__ import print_function # Must be first import
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as font
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
PYTHON_VER="3"
except ImportError: # Python 2
import Tkinter as tk
import ttk
import tkFont as font
import tkFileDialog as filedialog
import tkMessageBox as messagebox
PYTHON_VER="2"
无论运行的是哪个Python版本,为了将来的校对或向后兼容,代码将始终是
messagebox.
的。我只需要在上面的现有代码中插入两行。
使用父窗口几何图形的消息框
''' At least one song must be selected '''
if self.play_song_count == 0:
messagebox.showinfo(title="No Songs Selected", \
message="You must select at least one song!", \
parent=self.toplevel)
return
如果歌曲计数为零,我已经有了返回的代码。所以我只需要在现有代码之间插入三行。
通过使用父窗口引用,您可以省去复杂的几何图形代码:
parent=self.toplevel
另一个好处是,如果父窗口在程序启动后被移动,您的消息框仍将出现在可预测的位置。
![]() |
耍酷的木瓜 · Python中的并发控制 - Jiajun的技术笔记 12 小时前 |
![]() |
谦虚好学的紫菜 · 推荐开源项目:Zeep - Python SOAP 客户端 10 小时前 |
![]() |
礼貌的消防车 · C#/.net程序调用python - 步、步、为营 6 小时前 |
![]() |
旅行中的硬盘 · PAT考试_浙大pat一年可以考几次-CSDN博客 6 月前 |