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

时间:2022-12-24 12:15:55

I am looking for Growl-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:

我在Python中寻找类似Growl的Windows气球提示通知库。想象一下编写如下代码:

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

.. and that would notify with corresponding tooltips on Mac, Windows and Linux. Does such a library exist? If not, how would I go about writing one myself?

..并且会通过Mac,Windows和Linux上的相应工具提示进行通知。这样的图书馆存在吗?如果没有,我将如何自己写一个?

  • Does Mac come with a default notifier? Is Growl something that I should install separately?
  • Mac是否附带默认通知程序?是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' (i.e., 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框架来完成这样的简单任务。

8 个解决方案

#1


19  

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. 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.

这是我几年前使用wxPython编写的桌面通知程序 - 它在Windows和Linux上的行为相同,也应该在OSX上运行。它包含一个线程事件循环,可用于为包含可单击的图标和消息的通知窗口设置动画。它可能需要进行一些调整才能为您自己的目的进行定制,但是基础工作已经完成。

#2


17  

At Pycon 2010 there was a presentation on cross-platform Python development. There was a html page about it as well, containing some advice for cross-platform notification. However, I don't find it online anymore, but I saved a local copy, and this is the part on notifications:

在Pycon 2010上有一个关于跨平台Python开发的演示文稿。还有一个关于它的html页面,其中包含一些跨平台通知的建议。但是,我不再在网上找到它了,但我保存了一份本地副本,这是通知的一部分:

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 Windows:一个好的wxPython解决方案是Andrea Gavana的ToasterBox,它模仿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. The code is also very clear, so you'll quickly see what it does.

链接的python文件非常有趣,我认为你应该能够按原样使用链接的python文件。代码也非常清晰,因此您可以快速了解它的作用。

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 e.g. 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.

您可以对其进行调整,以便为上述三种通知系统提供支持。

#3


7  

  • 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)

    检查密钥环如何处理跨平台问题(它是一个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. Respectively, KNotification for KDE and libnotify for Gnome.

    对于unix,你可能想要像已经提到的那样挂钩到DBus(作为后备,注意dbus也可能在OSX中可用),但是KDE和Gnome都有类似Growl的库。 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来访问它们。

#4


3  

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

#5


2  

Sounds like you need Growl for Windows

听起来你需要Growl for Windows

#6


1  

Here is something simple that works for me. The Toast stays up for 2 secs and disappears. Yes, OP didn't want 'gigantic' PyQt4, but this may be useful to others.

这是一个简单的东西,适合我。吐司熬夜2秒后消失。是的,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)

#7


0  

For good cross-platform support, I would look at PyQt. It will add some heft to your library, but they've done a good job working out most of the kinks.

为了获得良好的跨平台支持,我会看一下PyQt。它会为你的图书馆增加一些重量,但是他们在解决大部分问题方面做得很好。

#8


0  

On win you can use snarl.

赢了,你可以使用咆哮。

Using it with python: www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2972

与python一起使用:www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2972

#1


19  

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. 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.

这是我几年前使用wxPython编写的桌面通知程序 - 它在Windows和Linux上的行为相同,也应该在OSX上运行。它包含一个线程事件循环,可用于为包含可单击的图标和消息的通知窗口设置动画。它可能需要进行一些调整才能为您自己的目的进行定制,但是基础工作已经完成。

#2


17  

At Pycon 2010 there was a presentation on cross-platform Python development. There was a html page about it as well, containing some advice for cross-platform notification. However, I don't find it online anymore, but I saved a local copy, and this is the part on notifications:

在Pycon 2010上有一个关于跨平台Python开发的演示文稿。还有一个关于它的html页面,其中包含一些跨平台通知的建议。但是,我不再在网上找到它了,但我保存了一份本地副本,这是通知的一部分:

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 Windows:一个好的wxPython解决方案是Andrea Gavana的ToasterBox,它模仿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. The code is also very clear, so you'll quickly see what it does.

链接的python文件非常有趣,我认为你应该能够按原样使用链接的python文件。代码也非常清晰,因此您可以快速了解它的作用。

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 e.g. 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.

您可以对其进行调整,以便为上述三种通知系统提供支持。

#3


7  

  • 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)

    检查密钥环如何处理跨平台问题(它是一个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. Respectively, KNotification for KDE and libnotify for Gnome.

    对于unix,你可能想要像已经提到的那样挂钩到DBus(作为后备,注意dbus也可能在OSX中可用),但是KDE和Gnome都有类似Growl的库。 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来访问它们。

#4


3  

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

#5


2  

Sounds like you need Growl for Windows

听起来你需要Growl for Windows

#6


1  

Here is something simple that works for me. The Toast stays up for 2 secs and disappears. Yes, OP didn't want 'gigantic' PyQt4, but this may be useful to others.

这是一个简单的东西,适合我。吐司熬夜2秒后消失。是的,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)

#7


0  

For good cross-platform support, I would look at PyQt. It will add some heft to your library, but they've done a good job working out most of the kinks.

为了获得良好的跨平台支持,我会看一下PyQt。它会为你的图书馆增加一些重量,但是他们在解决大部分问题方面做得很好。

#8


0  

On win you can use snarl.

赢了,你可以使用咆哮。

Using it with python: www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2972

与python一起使用:www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2972