2010-02-10 100 views
46

我正在尋找Python中類似於Windows氣球提示的通知庫Growl。想象一下,編寫如下代碼:Python中的跨平臺桌面通知器

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

..並且會在Mac,Windows和Linux上通過相應的工具提示進行通知。這樣的圖書館是否存在?如果不是,我會如何去寫一個自己?

  • Mac是否附帶默認通知程序? Growl是我應該單獨安裝的東西嗎?
  • 在Windows上,我認爲這可能需要pywin32
  • 在Linux上,假設GNOME有GNOME API(使用gnome-python)嗎?
  • 我可以在所有平臺上使通知「粘性」(即不會淡出)嗎?

更新:我的選擇是不依賴於巨大的GUI框架,如PyQt4的和wxPython的一個簡單的任務,如這一點。

+2

需要注意的是在OSX,現在有一個默認的通知和低吼已經成爲商業(因此大多不規範)。看[這裏](http://stackoverflow.com/questions/11814903/send-notification-to-mountain-lion-notification-center)。或[在這裏](https://github.com/albertz/music-player/blob/master/notifications.py)獲取Python實現。 – Albert 2012-09-02 05:53:43

回答

0

對於良好的跨平臺支持,我會看看PyQt。它會爲你的圖書館增加一些分量,但他們在解決大部分問題方面做得很好。

+0

PyQT對我的簡單需求太多了。此外,用PyQT部署應用程序是一件痛苦的事情:http://arstechnica.com/open-source/guides/2009/03/how-to-deploying-pyqt-applications-on-windows-and-mac-os- x.ars – 2010-02-11 05:32:30

7
  • 如何去寫它

    檢查如何keyring處理跨平臺問題(這是一個Python庫它插入各種自動檢測鑰匙鏈後端的存儲)

  • Growl未與OSX捆綁在一起,您必須單獨安裝它,OSX不附帶任何內置通知系統。對於unix,您可能想要像前面提到的那樣掛鉤DBus(作爲後備,請注意,dbus也可能在OSX中可用),但KDE和Gnome都有類似Growl的庫。分別爲KDE和GNOME的libnotify KNotification。

  • 對於Windows,檢查了咆哮,依傍如果沒有可用的(採用沿ToasterBox東西線),以通知氣泡

不會永遠甚至想使通知粘。它很愚蠢,它不敏感,而且很煩人。此外,由於像你這樣的人,在大多數通知系統中已經制作了而不是

最後,即使沒有任何這些的Python lib,您也可以使用​​來訪問它們。

+0

這正是我如何處理這個,如果沒有它的模塊。 upvoted讀心術。現在只有人需要編碼... PS:我個人使用Qt的泡沫作爲回落到處。它們很醜,但我寫的每個GUI應用程序都使用qt,所以它至少始終可用。 – 2011-07-31 10:40:01

+2

「* OSX不附帶任何內置通知系統*」不適用於最新的OSX版本。 – 2013-01-11 17:19:46

19

Here's a desktop notifier I wrote a few years ago using wxPython - 它在Windows和Linux中的行爲相同,也應該在OSX上運行。它包含一個線程事件循環,可用於爲包含可點擊的圖標和消息的通知窗口設置動畫。它可能需要一些調整來爲您自己的目的進行定製,但地面工作已完成。

+3

這是在什麼牌照下發布的?編輯:沒關係......這是一種BSD許可證......感謝你發佈代碼。找到我想從中學習的圖書館或東西是一種痛苦,不會冒任何未來我的代碼版本的風險。但是,我沒有問題,給予信貸,信貸是由於) – ThantiK 2010-02-11 18:03:12

+0

這個項目遷移到https://github.com/stephenmcd/readertray – tc88 2017-07-28 06:18:39

17

在2010 PYCON有一個presentation on cross-platform Python development。 還有一個關於它的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.

  • To keep notifications easy to port cross-platform, don't make them interactive. For example Ubuntu does not support notifications that require user interaction.
  • These are the most important libraries:

    o Linux: pynotify .

    o Mac OS X: Growl, which is not standard, is usually installed.

    o Windows: a good wxPython solution is ToasterBox of Andrea Gavana, which mimics the look of Firefox or Thunderbird notifications.

  • For Phatch we developed a library that unifies these three systems in one API: phatch/lib/notify.py .

鏈接的Python文件是非常有趣的,我想你應該能夠使用鏈接的python文件差不多如此。代碼也非常清晰,所以你很快就會看到它的功能。

的基本方法是檢測什麼通知系統可供選擇,幾乎不管是什麼平臺的,並試圖按照一定的順序使用它們,但如果有必要回落到更簡單的系統。這樣,如果用戶有例如咆哮安裝它會使用它,無論平臺。

您可以調整它以支持除上述三種之外的其他通知系統。

+1

我不知道爲什麼這個答案沒有更多upvotes。它開箱即用,非常容易理解。 – 2012-01-25 18:44:17

+1

嘗試notify.py。它需要使用'import other.pyWx.toasterbox'→'將wx.lib.agw.toasterbox導入爲TB'來更新,我相信。但它仍然沒有做任何事情:'發送('標題','消息')'→'PyNoAppError:必須先創建wx.App對象!' – endolith 2012-03-14 03:02:39

+0

本質上,我有無窗口的應用程序,我想說'氣球('東西')'而不是'print('something')''。爲什麼我們必須經歷所有創建和銷燬窗口以及彈出對話框或氣球的麻煩? http://stackoverflow.com/q/1635027/125507 – endolith 2012-03-14 03:06:01

1

這裏有一些簡單的東西適合我。吐司保持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 

的下調文件「uiToast.py」由pyuic4創建是:

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)