本文演示了如何创建一个简单的 桌面通知程序 应用程序使用Python。
桌面通知程序是一个简单的 在桌面上以弹出消息的形式生成通知消息的应用程序。
通知内容
在本文使用的示例中,桌面上显示为通知的内容是 头条新闻 今天最重要的。
因此,为了获取头条新闻,我们将使用以下Python脚本来获取新闻头条:
import requests import xml.etree.ElementTree as ET # url of news rss feed def loadRSS(): ''' utility function to load RSS feed ''' # create HTTP request response object resp = requests.get(RSS_FEED_URL) # return response content return resp.content def parseXML(rss): ''' utility function to parse XML format rss feed ''' # create element tree root object root = ET.fromstring(rss) # create empty list for news items newsitems = [] # iterate news items for item in root.findall( './channel/item' ): news = {} # iterate child elements of item for child in item: # special checking for namespace object content:media news[ 'media' ] = child.attrib[ 'url' ] else : news[child.tag] = child.text.encode( 'utf8' ) newsitems.append(news) # return news items list return newsitems def topStories(): ''' main function to generate and return news items ''' # load rss feed rss = loadRSS() # parse XML newsitems = parseXML(rss) return newsitems |
这是一个简单的Python脚本,可以解析XML格式的新闻标题。
注: 要了解XML解析的工作原理,请参阅本文: Python中的XML解析
上述Python脚本生成的示例新闻项如下所示:
{'description': 'Months after it was first reported, the feud between Dwayne Johnson and
Vin Diesel continues to rage on, with a new report saying that the two are
being kept apart during the promotions of The Fate of the Furious.',
'link': 'http://www.hindustantimes.com/hollywood/vin-diesel-dwayne-johnson-feud-rages-
on-they-re-being-kept-apart-for-fast-8-tour/story-Bwl2Nx8gja9T15aMvcrcvL.html',
'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/p2/2017/04/01/Pictures
/_fbcbdc10-1697-11e7-9d7a-cd3db232b835.jpg',
'pubDate': b'Sat, 01 Apr 2017 05:22:51 GMT ',
'title': "Vin Diesel, Dwayne Johnson feud rages on; they're being deliberately kept apart"}
将此Python脚本另存为 头条新闻。py (当我们在桌面通知应用程序中以这个名称导入时)。
装置
现在,为了创建桌面通知程序,需要安装第三方Python模块, 通知2 .
你可以安装 通知2 使用简单的pip命令:
pip install notify2
桌面通知应用
现在,我们为桌面通知程序编写Python脚本。
考虑下面的代码:
import time import notify2 from topnews import topStories # path to notification window icon ICON_PATH = "put full path to icon image here" # fetch news items newsitems = topStories() # initialise the d-bus connection notify2.init( "News Notifier" ) # create Notification object n = notify2.Notification( None , icon = ICON_PATH) # set urgency level n.set_urgency(notify2.URGENCY_NORMAL) # set timeout for a notification n.set_timeout( 10000 ) for newsitem in newsitems: # update notification data for Notification object n.update(newsitem[ 'title' ], newsitem[ 'description' ]) # show notification on screen n.show() # short delay between notifications time.sleep( 15 ) |
让我们试着一步一步地分析上面的代码:
- 在发送任何通知之前,我们需要初始化D总线连接。D-Bus是一种消息总线系统,是应用程序相互通信的简单方式。因此,当前Python脚本中notify2的D-Bus连接是通过以下方式初始化的:
notify2.init("News Notifier")
在这里,我们通过的唯一论点是 应用程序名 .您可以设置任意应用程序名称。
- 现在,我们创建一个通知对象, N 使用:
n = notify2.Notification(None, icon = ICON_PATH)
上述方法的通用语法为:
notify2.Notification(summary, message='', icon='')
在这里
- 总结: 标题文本
- 信息: 正文
- 偶像: 图标图像的路径
目前,我们已经设置了 总结 像 没有一个 并且通过了考试 图标路径 像 偶像 论点
注: 您需要传递图标图像的完整路径。
- 您可以选择使用设置通知的紧急级别 设定紧急状态 方法:
n.set_urgency(notify2.URGENCY_NORMAL)
可用常数为:
- 通知2。紧急程度低
- 通知2。紧急情况正常吗
- 通知2。紧急情况危急
- 另一个可选实用程序是 设置超时 方法,可以显式设置显示持续时间(以毫秒为单位),如下所示:
n.set_timeout(10000)
- 现在,当我们逐一遍历每个新闻条目时,我们需要用一个新的 总结 和 消息 使用 使现代化 方法:
n.update(newsitem['title'], newsitem['description'])
- 要显示通知,只需调用 show() 通知对象的方法如下:
n.show()
在Python脚本上运行时桌面的示例屏幕截图:
此桌面通知程序应用程序的Github存储库: 桌面通知程序示例
本博客由 尼希尔·库马尔 .如果你喜欢GeekSforgek并想投稿,你也可以使用“投稿”撰写文章。极客。组织或邮寄你的文章到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。