如何使用Python在系统托盘中创建弹出消息?(Windows)

48

我想了解如何使用Python创建系统托盘弹出消息。我在许多软件中看到过这些消息,但是很难找到可以轻松使用任何语言来实现它的资源。有人知道用Python实现这个功能的库吗?

7个回答

55
通过pywin32的帮助,您可以使用我在这里找到的以下示例代码:
from win32api import *
from win32gui import *
import win32con
import sys, os
import struct
import time
 
class WindowsBalloonTip:
    def __init__(self, title, msg):
        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
        }
        # Register the Window class.
        wc = WNDCLASS()
        hinst = wc.hInstance = GetModuleHandle(None)
        wc.lpszClassName = "PythonTaskbar"
        wc.lpfnWndProc = message_map # could also specify a wndproc.
        classAtom = RegisterClass(wc)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = CreateWindow( classAtom, "Taskbar", style, \
                0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
                0, 0, hinst, None)
        UpdateWindow(self.hwnd)
        iconPathName = os.path.abspath(os.path.join( sys.path[0], "balloontip.ico" ))
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        try:
           hicon = LoadImage(hinst, iconPathName, \
                    win32con.IMAGE_ICON, 0, 0, icon_flags)
        except:
          hicon = LoadIcon(0, win32con.IDI_APPLICATION)
        flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "tooltip")
        Shell_NotifyIcon(NIM_ADD, nid)
        Shell_NotifyIcon(NIM_MODIFY, \
                         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
                          hicon, "Balloon  tooltip",msg,200,title))
        # self.show_balloon(title, msg)
        time.sleep(10)
        DestroyWindow(self.hwnd)
    def OnDestroy(self, hwnd, msg, wparam, lparam):
        nid = (self.hwnd, 0)
        Shell_NotifyIcon(NIM_DELETE, nid)
        PostQuitMessage(0) # Terminate the app.

def balloon_tip(title, msg):
    w=WindowsBalloonTip(title, msg)

if __name__ == '__main__':
    balloon_tip("Title for popup", "This is the popup's message")

可以工作,但我希望它更加“现代化”,外观和感觉不同。例如,能否在弹出窗口中添加图像?这种方法是否可行? - Roman Rdgz
1
还没有尝试过,但是也许在iconPathName的位置提供图标路径可能会起作用。 - otaku
2
这个方法是可行的,但只有在给 lpszClassName 分配一个随机类名时才有效。如果按原样运行示例,则仅会显示一个气泡帮助,然后在下一次尝试时失败,并显示 pywintypes.error: (1410, 'RegisterClass', 'Class already exists')。作为快速解决方案,我使用了 from uuid import uuid4 并将 uuid4() 添加到名称中。 - 576i
我是编程新手,如何将此链接到我的GUI脚本?基本测试可以工作,但我很难看到一个函数,当双击系统托盘图标时,将打开程序。我还尝试使用pythonw打开它,但它仍然作为普通任务栏应用程序打开。 - ISquared

42

最近我使用了Plyer包来创建跨平台通知,使用Notification外观(它还有许多其他值得一看的有趣功能),而不会感到痛苦。

非常容易使用:

from plyer.utils import platform
from plyer import notification

notification.notify(
    title='Here is the title',
    message='Here is the message',
    app_name='Here is the application name',
    app_icon='path/to/the/icon.{}'.format(
        # On Windows, app_icon has to be a path to a file in .ICO format.
        'ico' if platform == 'win' else 'png'
    )
)

1
当我使用这个库时,我从python27_x64\lib\site-packages\plyer\platforms\win\libs\balloontip.py"的第145行,在方法WindowsBalloonTip(**kwargs)中收到了一个错误,错误信息是:TypeError: init() got an unexpected keyword argument 'ticker'`。它是在Windows7上使用Python 2.7。 - WesternGun
3
检查代码库后发现已经有类似的问题,并且作者建议直接从Github安装最新的开发版本来解决问题,这很好!pip install -I https://github.com/kivy/plyer/zipball/master - WesternGun
1
运行我的应用程序多次后,我发现通知可以正常工作而没有任何问题,但是后来它开始抛出异常[异常:Shell_NotifyIconW失败]。 - alex_z
根据plyer notification.notify函数实现中的注释,当在Windows上调用时,app_icon需要以.ico格式存在(而不是.png)。 - Thijs
@Thijs 是的,文档中也提到了:https://plyer.readthedocs.io/en/latest/#plyer.facades.Notification。我已更新我的示例脚本。 - Epoc

20

使用 Python 模块 win10toast 可以简单地在 Windows 10 上显示通知。

要求:

  • pypiwin32
  • setuptools

安装:

>> pip install win10toast

示例:

from win10toast import ToastNotifier
toaster = ToastNotifier()
toaster.show_toast("Demo notification",
                   "Hello world",
                   duration=10)

代码结果的图像


这个模块支持win8/7吗? - Exil
1
@ExillustX 我没有在win8/7上尝试过,但我认为PyQt5包在win8/7上提供了类似的行为。请检查一下。 - Ananth Kumar Vasamsetti
我们能否设定一个弹出时间?另外,我们能否将所有通知保存在一个地方?比如一个文件? - Anurag A S
这导致我的程序崩溃了。 - cslotty
@ExillustX 我在 Windows 7 上运行它,一切正常。但是在 Windows 10 上通知有所不同。 - Nima

9

Windows

现在有一种官方方法可以使用Python/Winrt实现,该Github解释了如何将UWP API映射到Python API。

通过遵循官方的UWP文档,我成功地显示了一个小通知,它也出现在Windows的通知中心:

import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom

#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier();

#define your notification as string
tString = """
<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>Sample toast</text>
            <text>Sample content</text>
        </binding>
    </visual>
</toast>
"""

#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

#display notification
notifier.show(notifications.ToastNotification(xDoc))

安装仅限于库的安装。
pip install winrt

需求

Windows 10,2018年10月更新或更高版本

适用于Windows的Python,版本为3.7或更高

pip,版本为19或更高

额外奖励 macOS

我也找到了一种在macOS中使用AppleScript的方法,以下代码的目标是构建一个将通过python os.system执行的AppleScript代码。

import os

def displayNotification(message,title=None,subtitle=None,soundname=None):
    """
        Display an OSX notification with message title an subtitle
        sounds are located in /System/Library/Sounds or ~/Library/Sounds
    """
    titlePart = ''
    if(not title is None):
        titlePart = 'with title "{0}"'.format(title)
    subtitlePart = ''
    if(not subtitle is None):
        subtitlePart = 'subtitle "{0}"'.format(subtitle)
    soundnamePart = ''
    if(not soundname is None):
        soundnamePart = 'sound name "{0}"'.format(soundname)

    appleScriptNotification = 'display notification "{0}" {1} {2} {3}'.format(message,titlePart,subtitlePart,soundnamePart)
    os.system("osascript -e '{0}'".format(appleScriptNotification))

使用 asis:
displayNotification("message","title","subtitle","Pop")

最后说明

我已经把之前的所有代码总结在两个代码片段中:

Windows

macOS


1
来自微软的爱? - ChipJust
1
notifier = nManager.create_toast_notifier() # 抛出异常 Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: 元素未找到。 - ChipJust
看起来最近的Windows更新(或API /配置更改)破坏了这段代码。(其他开发人员在我的gist评论部分报告了相同的行为) - Marc_Alx
1
有没有办法将 Toast 通知的按钮连接到一个操作上?或者有没有办法获取 Toast 通知的输入内容? - Andy_ye
在3.11中,winrt似乎无法通过pip进行安装,并且看起来已经不再得到维护。不过有一个社区支持的分支,名为winsdk。 - BoZenKhaa

6
你需要使用第三方Python GUI库或Pywin32库。自带于Python的GUI工具包TkInter不支持系统托盘弹出窗口。
支持与系统托盘操作配合的多平台中立库:
- wxPython - PyGTK - PyQT
支持Windows系统托盘操作的专用库:
- pywin32
通过wxPython在Windows上实现系统托盘弹出窗口的示例及信息:
- 无法正确使用wx.NotificationMessage和wxPython:Unable to use wx.NotificationMessage properly with wxPython

好的,有使用wxPython的示例吗? - Roman Rdgz
1
@RomanRdgz 你可以看一下我在https://dev59.com/5HE95IYBdhLWcg3wmvCh找到的[ToasterBox](http://xoomer.virgilio.it/infinity77/main/freeware.html#toasterbox)。 - halex

5
在Linux系统中,您可以使用内置命令notify-send
可以使用ntfy库发送推送通知。 点击此处查看ntfy文档 安装:
sudo pip install ntfy

例子:

ntfy send "your message!"
ntfy -t "your custom title" send "your message"

1
我想补充一点,这是一个从命令行调用的Python程序,而不是内部Python命令。 - tc88

-1

最简单的方法是使用win10toast

这是代码:

from win10toast import ToastNotifier

Notifi = ToastNotifier()  
Notifi.show_toast("Title", "Description")

这可能会对你有所帮助!


1
与Ananth Kumar Vasamsetti的答案相比,这如何提供新的价值? - Jan Wilamowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接