如何使用python创建系统托盘弹出消息?(窗口)
我想知道如何使用python创建系统托盘弹出消息。我在很多softaware中看到过这些,但是很难找到资源来轻松地用任何语言实现。任何人都知道用Python执行此操作的库吗?在的帮助下,您可以使用我找到的以下示例代码:如何使用python创建系统托盘弹出消息?(窗口),python,popup,system-tray,Python,Popup,System Tray,我想知道如何使用python创建系统托盘弹出消息。我在很多softaware中看到过这些,但是很难找到资源来轻松地用任何语言实现。任何人都知道用Python执行此操作的库吗?在的帮助下,您可以使用我找到的以下示例代码: 您需要使用第三方python GUI库或pywin32库。TkInter是python附带的GUI工具包,它不支持系统托盘弹出窗口 支持使用系统托盘的多种形式的中性库: wxPython 皮格特克 PyQT 支持使用系统托盘的Windows特定库: pywin32 在wi
您需要使用第三方python GUI库或pywin32库。TkInter是python附带的GUI工具包,它不支持系统托盘弹出窗口 支持使用系统托盘的多种形式的中性库:
- wxPython
- 皮格特克
- PyQT
- pywin32
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.' + ('ico' if platform == 'win' else 'png')
)
在Linux系统中,您可以使用内置命令
notifysend
ntfy
库可用于发送推送通知
安装:
sudo pip install ntfy
示例:
ntfy send "your message!"
ntfy -t "your custom title" send "your message"
下面是使用python在windows 10上显示通知的简单方法:modulewin10toast 要求:
>> pip install win10toast
from win10toast import ToastNotifier
toaster = ToastNotifier()
toaster.show_toast("Demo notification",
"Hello world",
duration=10)
- pypiwin32
- 设置工具
>> pip install win10toast
from win10toast import ToastNotifier
toaster = ToastNotifier()
toaster.show_toast("Demo notification",
"Hello world",
duration=10)
示例:
>> pip install win10toast
from win10toast import ToastNotifier
toaster = ToastNotifier()
toaster.show_toast("Demo notification",
"Hello world",
duration=10)
Windows 现在有了一种官方的方法来实现这一点,github解释了如何将UWPAPI映射到python API 通过执行以下操作,我成功地显示了一个也出现在Windows notification center中的小通知:
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))
要求
Windows 10,2018年10月更新或更高版本
Python for Windows,3.7版或更高版本
pip,版本19或更高版本
红利macOS
我还发现了一种在macOS中使用AppleScript的方法,下面代码的目标是构建一个AppleScript代码,该代码将通过pythonos.system
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")
最终注释
我用两个GIST总结了前面的代码
好的,有没有关于如何使用wxPython的例子?@RomandGz看看我在.Works中找到的,但我希望它更“现代”,外观和感觉不同。例如,能够在弹出窗口中添加图像。使用此方法是否可行?尚未尝试,但可能会使用图标路径代替iconPathName。此方法可行,但仅当您为
lpszClassName
指定随机类名时。如果按原样运行该示例,它将只显示一个气泡帮助,然后在下一次尝试时使用pywintypes.error:(1410,'RegisterClass','Class ready exists')
失败。作为一个快速破解,我使用了uuid导入uuid4的,并在名称中添加了uuid4()
。我是编程新手,如何将其链接到我的GUI脚本?基本测试工作正常,但我很难看到一个功能,双击系统托盘图标,就会打开程序。我还尝试使用pythonw打开它,但它仍然作为一个普通的任务栏应用程序打开。我要补充的是,这是一个从命令行调用的python程序,而不是内部python命令。当我使用这个库时,我从python27\u x64\lib\site packages\plyer\platforms\win\libs\balloottip.py中得到一个错误“,第145行,在方法WindowsBallootTip(**kwargs)
的balloon_tip
中,错误消息是:TypeError:\uuu init_uuu()获得了意外的关键字参数“ticker”
。它是windows7和python 2.7。。。。在查看repo时,已经发布了一个类似的问题,作者建议直接从Github安装最新的dev版本,这就解决了问题。太好了<代码>pip安装-Ihttps://github.com/kivy/plyer/zipball/master
遇到了一个问题,在运行了几次my app之后,我得到了运行没有任何问题的通知,但是,根据plyer notification.notifiy函数实现中的注释,后来它开始抛出异常[exception:Shell\u NotifyIconW failed.],在Windows上调用app_图标时,需要使用.ICO格式(而不是.png)。@Thijs是的,文档中也提到了它:。更新了我的示例脚本。此模块是否支持win8/7?@ExillustX我还没有在win8/7上尝试过,我认为PyQt5包在两个win8/7上都提供了类似的行为。一定要检查一下。我们能告诉你什么时候会出现这个吗?我们还可以将所有通知保存在一个地方吗?可能是一个文件?这使我的程序崩溃。@ExillustX我在Windows 7上运行了它,它运行得很好。但它与Windows 10 Notifications不同,它来自Microsoft with love?notifier=nManager。create_toast_notifier()#引发异常回溯(最近一次调用最后一次):file“”,第1行,在RuntimeError中:找不到元素。实际上,最近的windows更新(或API/配置更改)似乎破坏了此代码。(其他开发人员在我的要点的评论部分报告了相同的行为)有没有办法将toast通知的按钮连接到操作?或者有没有办法获取toast通知的输入?