這裏有一些簡單的東西適合我。吐司保持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)
需要注意的是在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