Python 中的警报框?

发布于 2024-07-07 00:45:11 字数 232 浏览 7 评论 0原文

是否有可能生成类似于Python中JavaScript的alert(“message”)的警报,并且应用程序作为守护进程运行。

这将在 Windows 中运行,很可能是 XP,但 2000 和 Vista 也非常有可能。

更新:
这是为了在后台运行并在满足某些条件时提醒用户,我认为提醒用户的最简单方法是生成一个弹出窗口,因为它需要立即处理,以及其他选项,例如仅仅登录或发送电子邮件是不够有效的。

Is it possible to produce an alert similar to JavaScript's alert("message") in python, with an application running as a daemon.

This will be run in Windows, Most likely XP but 2000 and Vista are also very real possibilities.

Update:
This is intended to run in the background and alert the user when certain conditions are met, I figure that the easiest way to alert the user would be to produce a pop-up, as it needs to be handled immediately, and other options such as just logging, or sending an email are not efficient enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

太傻旳人生 2024-07-14 00:45:11

GTK 可能是一个更好的选择,因为它是跨平台的。 它在 Ubuntu 上运行得很好,并且在安装了 GTK 和 Python 绑定后在 Windows 上也应该运行得很好。

from gi.repository import Gtk

dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK, "This is an INFO MessageDialog")
dialog.format_secondary_text(
    "And this is the secondary text that explains things.")
dialog.run()
print "INFO dialog closed"

您可以在此处查看其他示例。 (pdf)

传递的参数应该是 gtk.window 父级(或无)、DestroyWithParent、消息类型、消息按钮、标题。

GTK may be a better option, as it is cross-platform. It'll work great on Ubuntu, and should work just fine on Windows when GTK and Python bindings are installed.

from gi.repository import Gtk

dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK, "This is an INFO MessageDialog")
dialog.format_secondary_text(
    "And this is the secondary text that explains things.")
dialog.run()
print "INFO dialog closed"

You can see other examples here. (pdf)

The arguments passed should be the gtk.window parent (or None), DestroyWithParent, Message type, Message-buttons, title.

说不完的你爱 2024-07-14 00:45:11

将应用程序作为后台进程启动,该进程要么具有绑定到本地主机的 TCP 端口,要么通过文件进行通信 - 您的守护进程打开文件,然后您 echo "foo" > c:\你的\文件。 比如说,1 秒无活动后,您将显示消息并截断文件。

Start an app as a background process that either has a TCP port bound to localhost, or communicates through a file -- your daemon has the file open, and then you echo "foo" > c:\your\file. After, say, 1 second of no activity, you display the message and truncate the file.

眉黛浅 2024-07-14 00:45:11

您可以在Python中使用win32库,这是确定或取消的经典示例。

import win32api
import win32com.client
import pythoncom

result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)

if result == 1:
 print 'Ok'
elif result == 2:
 print 'cancel'

集合:

win32api.MessageBox(0,"msgbox", "title")
win32api.MessageBox(0,"ok cancel?", "title",1)
win32api.MessageBox(0,"abort retry ignore?", "title",2)
win32api.MessageBox(0,"yes no cancel?", "title",3)

You can use win32 library in Python, this is classical example of OK or Cancel.

import win32api
import win32com.client
import pythoncom

result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)

if result == 1:
 print 'Ok'
elif result == 2:
 print 'cancel'

The collection:

win32api.MessageBox(0,"msgbox", "title")
win32api.MessageBox(0,"ok cancel?", "title",1)
win32api.MessageBox(0,"abort retry ignore?", "title",2)
win32api.MessageBox(0,"yes no cancel?", "title",3)
难得心□动 2024-07-14 00:45:11

怎么样:

import win32api

win32api.MessageBox(0, 'hello', 'title')

另外:

win32api.MessageBox(0, 'hello', 'title', 0x00001000) 

将使该框出现在其他窗口的顶部,以显示紧急消息。 请参阅 MessageBox 函数其他选项。

what about this:

import win32api

win32api.MessageBox(0, 'hello', 'title')

Additionally:

win32api.MessageBox(0, 'hello', 'title', 0x00001000) 

will make the box appear on top of other windows, for urgent messages. See MessageBox function for other options.

唱一曲作罢 2024-07-14 00:45:11

对于我们这些寻找不与 Windows 交互且独立于平台的纯 Python 选项的人来说,我选择了以下网站上列出的选项:

https://pythonspot.com/tk-message-box/ (存档链接:https://archive.ph/JNuvx

# Python 3.x code
# Imports
import tkinter
from tkinter import messagebox

# This code is to hide the main tkinter window
root = tkinter.Tk()
root.withdraw()

# Message Box
messagebox.showinfo("Title", "Message")

您可以选择针对不同场景显示各种类型的消息框选项:

  • showinfo()
  • showwarning()
  • showerror ()
  • Askquestion()
  • Askokcancel()
  • askyesno ()
  • Askretrycancel ( )

根据我下面的评论编辑了代码

For those of us looking for a purely Python option that doesn't interface with Windows and is platform independent, I went for the option listed on the following website:

https://pythonspot.com/tk-message-box/ (archived link: https://archive.ph/JNuvx)

# Python 3.x code
# Imports
import tkinter
from tkinter import messagebox

# This code is to hide the main tkinter window
root = tkinter.Tk()
root.withdraw()

# Message Box
messagebox.showinfo("Title", "Message")

You can choose to show various types of messagebox options for different scenarios:

  • showinfo()
  • showwarning()
  • showerror ()
  • askquestion()
  • askokcancel()
  • askyesno ()
  • askretrycancel ()

edited code per my comment below

孤独患者 2024-07-14 00:45:11

您可以使用 PyAutoGui 制作警报框
首先用 pip 安装 pyautogui:

pip install pyautogui

然后在 python 中输入:

import pyautogui as pag
pag.alert(text="Hello World", title="The Hello World Box")

这里有更多消息框,从 Javascript 中窃取:

  • confirm()
    使用“确定”和“取消”按钮
  • prompt()
    使用文本输入
  • password()
    使用文本输入,但键入的字符将显示为 *

You can use PyAutoGui to make alert boxes
First install pyautogui with pip:

pip install pyautogui

Then type this in python:

import pyautogui as pag
pag.alert(text="Hello World", title="The Hello World Box")

Here are more message boxes, stolen from Javascript:

  • confirm()
    With Ok and Cancel Button
  • prompt()
    With Text Input
  • password()
    With Text Input, but typed characters will be appeared as *
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文