简体   繁体   English

Python中的跨平台桌面通知程序

[英]Cross-platform desktop notifier in Python

I am looking for Growl -like, Windows balloon-tip-like notifications library in Python. 我在Python中寻找类似Growl的,类似于气球提示的通知库。 Imagine writing code like: 想象一下编写如下代码:

>>> import desktopnotifier as dn
>>> dn.notify('Title', 'Long description goes here')

.. and that would notify with corresponding tooltips on Mac, Windows and Linux. ..并且会通过Mac,Windows和Linux上的相应工具提示进行通知。 Does such a library exist? 这样的图书馆存在吗? If not, how would I go about writing one myself? 如果没有,我将如何自己写一个?

  • Does Mac come with a default notifier? Mac是否附带默认通知程序? Is Growl something that I should install separately? Growl我应该分开安装吗?
  • On Windows, I assume this may require pywin32 ? 在Windows上,我认为这可能需要pywin32
  • On Linux, assuming GNOME, is there a GNOME API (using gnome-python) that does this? 在Linux上,假设GNOME,是否有GNOME API(使用gnome-python)来做到这一点?
  • Can I make notifications 'sticky' (ie, don't fade out ever) on all platforms? 我可以在所有平台上发出“粘性”通知(即,不要淡出)吗?

Update : My preference is to not depend on gigantic GUI frameworks like PyQT4 and wxPython for a simple task such as this. 更新 :我的偏好是依赖于像PyQT4和wxPython这样的巨大GUI框架来完成这样的简单任务。

Here's a desktop notifier I wrote a few years ago using wxPython - it behaves identically across Windows and Linux and should also run on OSX. 这是我几年前使用wxPython编写的桌面通知程序 - 它在Windows和Linux上的行为相同,也应该在OSX上运行。 It contains a threaded event loop that can be used to animate a notification window containing an icon and message that can be clicked on. 它包含一个线程事件循环,可用于为包含可单击的图标和消息的通知窗口设置动画。 It probably needs a couple of tweaks to customize it for your own purpose but the ground work is done. 它可能需要进行一些调整才能为您自己的目的进行自定义,但是基础工作已经完成。

At Pycon 2010 there was a presentation on cross-platform Python development . 在Pycon 2010上有一个关于跨平台Python开发演示文稿 There was a html page about it as well, containing some advice for cross-platform notification. 还有一个关于它的html页面,其中包含一些跨平台通知的建议。 However, I don't find it online anymore, but I saved a local copy, and this is the part on notifications: 但是,我不再在网上找到它,但我保存了一份本地副本,这是通知的一部分:

There are occasions in which your application wants to notify the user about something: software updates are available, a new instant message have been received, the 300 page print job has finally finished, etc. 在某些情况下,您的应用程序想要通知用户有关某些事情:软件更新可用,已收到新的即时消息,300页打印作业已完成,等等。

  • To keep notifications easy to port cross-platform, don't make them interactive. 要使通知易于跨平台移植,请不要使它们具有交互性。 For example Ubuntu does not support notifications that require user interaction. 例如,Ubuntu不支持需要用户交互的通知。
  • These are the most important libraries: 这些是最重要的图书馆:

    o Linux: pynotify . o Linux: pynotify

    o Mac OS X: Growl, which is not standard, is usually installed. o Mac OS X:通常会安装非标准的Growl。

    o Windows: a good wxPython solution is ToasterBox of Andrea Gavana, which mimics the look of Firefox or Thunderbird notifications. O窗口:一个很好的wxPython的解决方案是ToasterBox安德烈Gavana,它模仿的Firefox或Thunderbird通知的样子。

  • For Phatch we developed a library that unifies these three systems in one API: phatch/lib/notify.py . 对于Phatch,我们开发了一个库,在一个API中统一这三个系统: phatch / lib / notify.py

The linked python file is very interesting, and I think you should be able to use the linked python file almost as is. 链接的python文件非常有趣,我认为你应该能够使用链接的python文件。 The code is also very clear, so you'll quickly see what it does. 代码也很清晰,所以你很快就会看到它的作用。

The basic approach is it detects what notification systems are available, almost regardless of the platform, and tries to use them in a certain order but falls back to more simple systems if necessary. 基本方法是检测哪些通知系统可用,几乎与平台无关,并尝试按特定顺序使用它们,但如有必要,可以回退到更简单的系统。 This way, if the user has eg Growl installed it'll use it, regardless of the platform. 这样,如果用户安装了例如Growl,它将使用它,而不管平台如何。

You could adapt it to provide support for other notification systems than the three mentioned above. 您可以对其进行调整,以便为上述三种通知系统提供支持。

  • How to go about writing it 怎么去写它

    Check how keyring handles the cross-platform issues (it's a python library which plugs into various autodetected keychain backends for storage) 检查keyring如何处理跨平台问题(它是一个python库,可插入各种自动检测的钥匙串后端进行存储)

  • Growl isn't bundled with OSX you have to install it separately, OSX doesn't come with any built-in notification system. Growl不与OSX捆绑在一起,你必须单独安装它,OSX没有附带任何内置的通知系统。

  • For unixes, you might want to hook into DBus as already mentioned (as a fallback, note that dbus might also be available in OSX), but both KDE and Gnome have Growl-like libraries. 对于unix,你可能想要像已经提到的那样挂钩到DBus(作为后备,注意dbus也可能在OSX中可用),但是KDE和Gnome都有类似Growl的库。 Respectively, KNotification for KDE and libnotify for Gnome. KDE的KNotification和Gnome的libnotify。

  • For windows, check out Snarl, fall back to notification bubbles if not available (using something along the lines of ToasterBox ) 对于Windows,请查看Snarl,如果不可用,请回到通知气泡(使用ToasterBox行的内容

Do not ever even think of making notifications sticky. 永远不要想到通知粘性。 It's stupid, it's insensitive and it's freaking annoying. 这是愚蠢的,它不敏感,而且令人讨厌。 Also, due to people like you it's been made not available in most notification systems. 此外,由于像你这样的人,它在大多数通知系统中都不可用。

Finally, even if there is no Python lib for any of these, you can probably use ctypes to access them. 最后,即使没有任何Python lib,你也可以使用ctypes来访问它们。

try PyQt4 , if you don't care about the size. 试试PyQt4 ,如果你不在乎大小。

here is the class for this job: http://doc.trolltech.com/4.5/qsystemtrayicon.html 这是这份工作的课程: http//doc.trolltech.com/4.5/qsystemtrayicon.html

听起来你需要Growl for Windows

Here is something simple that works for me. 这是一个对我有用的简单方法。 The Toast stays up for 2 secs and disappears. 吐司熬夜2秒后消失。 Yes, OP didn't want 'gigantic' PyQt4, but this may be useful to others. 是的,OP不想要“巨大的”PyQt4,但这可能对其他人有用。

import sys, time
from PyQt4 import QtCore, QtGui
import uiToast

window = None   # global

# Usage: Toast('Message')
class Toast(QtGui.QMainWindow):
    def __init__(self, msg):
        global window               # some space outside the local stack
        window = self               # save pointer till killed to avoid GC
        QtGui.QWidget.__init__(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.ui = uiToast.Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.display.setText(msg)

        self.toastThread = ToastThread()    # start thread to remove display
        self.connect(self.toastThread, QtCore.SIGNAL("finished()"), self.toastDone)
        self.toastThread.start()
        self.show()

    def toastDone(self):
        global window
        window = None               # kill pointer to window object to close it and GC

class ToastThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        time.sleep(2.0)             # wait and die

The trimmed down file 'uiToast.py' created by pyuic4 is: pyuic4创建的精简文件'uiToast.py'是:

from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(547, 96)
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        MainWindow.setPalette(palette)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.display = QtGui.QTextBrowser(self.centralwidget)
        self.display.setGeometry(QtCore.QRect(0, 0, 551, 101))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        self.display.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.display.setFont(font)
        MainWindow.setCentralWidget(self.centralwidget)

For good cross-platform support, I would look at PyQt . 为了获得良好的跨平台支持,我会看一下PyQt It will add some heft to your library, but they've done a good job working out most of the kinks. 它会给你的图书馆增加一些重要性,但他们在解决大部分问题方面做得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM