我想了解如何使用Python创建系统托盘弹出消息。我在许多软件中看到过这些消息,但是很难找到可以轻松使用任何语言来实现它的资源。有人知道用Python实现这个功能的库吗?
我想了解如何使用Python创建系统托盘弹出消息。我在许多软件中看到过这些消息,但是很难找到可以轻松使用任何语言来实现它的资源。有人知道用Python实现这个功能的库吗?
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")
最近我使用了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'
)
)
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。 - WesternGunpip install -I https://github.com/kivy/plyer/zipball/master
。 - WesternGunWindows
现在有一种官方方法可以使用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))
displayNotification("message","title","subtitle","Pop")
最后说明
我已经把之前的所有代码总结在两个代码片段中:
notify-send
。ntfy
库发送推送通知。
点击此处查看ntfy文档
安装:sudo pip install ntfy
例子:
ntfy send "your message!"
ntfy -t "your custom title" send "your message"
最简单的方法是使用win10toast
这是代码:
from win10toast import ToastNotifier
Notifi = ToastNotifier()
Notifi.show_toast("Title", "Description")
这可能会对你有所帮助!
lpszClassName
分配一个随机类名时才有效。如果按原样运行示例,则仅会显示一个气泡帮助,然后在下一次尝试时失败,并显示pywintypes.error: (1410, 'RegisterClass', 'Class already exists')
。作为快速解决方案,我使用了from uuid import uuid4
并将uuid4()
添加到名称中。 - 576i