#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on 2018年9月9日 @author: Irony @site: https://github.com/892768447 @email: 892768447@qq.com @file: Notification @description: """ import base64 from PyQt5.QtCore import Qt, QRectF, QSize, pyqtSignal, QTimer from PyQt5.QtGui import QPixmap, QImage, QPainter, QPainterPath,\ QColor from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout,\ QGridLayout, QSpacerItem, QSizePolicy, QGraphicsDropShadowEffect,\ QListWidget, QListWidgetItem __Author__ = """By: Irony QQ: 892768447 Email: 892768447@qq.com""" __Copyright__ = 'Copyright (c) 2018 Irony' __Version__ = 1.0 class NotificationIcon: Info, Success, Warning, Error, Close = range(5) Types = { Info: None, Success: None, Warning: None, Error: None, Close: None } @classmethod def init(cls): cls.Types[cls.Info] = QPixmap(QImage.fromData(base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAC5ElEQVRYR8VX0VHbQBB9e/bkN3QQU0FMBSEVYFcQ8xPBJLJ1FWAqOMcaxogfTAWQCiAVRKkgTgfmM4zRZu6QhGzL0p0nDPr17e7bt7tv14RX/uiV48MJgAon+8TiAMRtMFogaqUJxADPwRRzg67kl8+xbWJWANR40iPQSSFgtX/mGQkaDr56V3VAKgGos4s2JXwJoF3naMPvMS+SrpTHs032GwGkdF+DsFMVnJm/oyGGeHico0EjIjpYes+YMyVd6R/flfkpBWCCQ9zaZM2LZDfLMGXsZ5kdI/lYBmINgHHyyLd1mWdBbAFAM/GY7K2WYx1AeB4T6L1N9umbGxZ0qktATaEAdCps48D39oq/LwEw3U5CN92LfczJoewfT7MAywDCaEbAuxeLrh0zz4L+0e4aAJfGy+sP3IMxlH1vpMJoSMCJDXgWtJeJVc6ACs9HBBrYODCJAFdYvAmkPJxnNqMwYht7Bn+T/lGg3z4DGEd3RPhQ54DBvwAOVkeqagRXfTLjh+x7+8sALOtfHLuiYzWOAiLoKbD58mnIGbCmLxUepS6NQmYlUGE0JeCTTXT9JvA9E9sZ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lt6wCgFEk0r1CIkbiPnC9DxH5aHcd4FYGD5MOqVOg/muslh0/vphkm63k5eXZvA0I6qD+ZCI3jDzLxANiHn1NNvb6+30aVYgwLeeUsgFW1svsPA3Ncq4MHzVeO8AAAAASUVORK5CYII='))) cls.Types[cls.Close] = QPixmap(QImage.fromData(base64.b64decode( 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAeElEQVQ4T2NkoBAwUqifgboGzJy76AIjE3NCWmL0BWwumzV/qcH/f38XpCfHGcDkUVwAUsDw9+8GBmbmAHRDcMlheAGbQnwGYw0DZA1gp+JwFUgKZyDCDQGpwuIlrGGAHHAUGUCRFygKRIqjkeKERE6+oG5eIMcFAOqSchGwiKKAAAAAAElFTkSuQmCC'))) @classmethod def icon(cls, ntype): return cls.Types.get(ntype) class NotificationItem(QWidget): closed = pyqtSignal(QListWidgetItem) def __init__(self, title, message, item, *args, ntype=0, callback=None, **kwargs): super(NotificationItem, self).__init__(*args, **kwargs) self.item = item self.callback = callback layout = QHBoxLayout(self, spacing=0) layout.setContentsMargins(0, 0, 0, 0) self.bgWidget = QWidget(self) # 背景控件, 用于支持动画效果 layout.addWidget(self.bgWidget) layout = QGridLayout(self.bgWidget) layout.setHorizontalSpacing(15) layout.setVerticalSpacing(10) # 标题左边图标 layout.addWidget( QLabel(self, pixmap=NotificationIcon.icon(ntype)), 0, 0) # 标题 self.labelTitle = QLabel(title, self) font = self.labelTitle.font() font.setBold(True) font.setPixelSize(22) self.labelTitle.setFont(font) # 关闭按钮 self.labelClose = QLabel( self, cursor=Qt.PointingHandCursor, pixmap=NotificationIcon.icon(NotificationIcon.Close)) # 消息内容 self.labelMessage = QLabel( message, self, cursor=Qt.PointingHandCursor, wordWrap=True, alignment=Qt.AlignLeft | Qt.AlignTop) font = self.labelMessage.font() font.setPixelSize(20) self.labelMessage.setFont(font) self.labelMessage.adjustSize() # 添加到布局 layout.addWidget(self.labelTitle, 0, 1) layout.addItem(QSpacerItem( 40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum), 0, 2) layout.addWidget(self.labelClose, 0, 3) layout.addWidget(self.labelMessage, 1, 1, 1, 2) # 边框阴影 effect = QGraphicsDropShadowEffect(self) effect.setBlurRadius(12) effect.setColor(QColor(0, 0, 0, 25)) effect.setOffset(0, 2) self.setGraphicsEffect(effect) self.adjustSize() # 5秒自动关闭 self._timer = QTimer(self, timeout=self.doClose) self._timer.setSingleShot(True) # 只触发一次 self._timer.start(5000) def doClose(self): try: # 可能由于手动点击导致item已经被删除了 self.closed.emit(self.item) except: pass def showAnimation(self, width): # 显示动画 pass def closeAnimation(self): # 关闭动画 pass def mousePressEvent(self, event): super(NotificationItem, self).mousePressEvent(event) w = self.childAt(event.pos()) if not w: return if w == self.labelClose: # 点击关闭图标 # 先尝试停止计时器 self._timer.stop() self.closed.emit(self.item) elif w == self.labelMessage and self.callback and callable(self.callback): # 点击消息内容 self._timer.stop() self.closed.emit(self.item) self.callback() # 回调 def paintEvent(self, event): # 圆角以及背景色 super(NotificationItem, self).paintEvent(event) painter = QPainter(self) path = QPainterPath() path.addRoundedRect(QRectF(self.rect()), 6, 6) painter.fillPath(path, Qt.white) class NotificationWindow(QListWidget): _instance = None def __init__(self, *args, **kwargs): super(NotificationWindow, self).__init__(*args, **kwargs) self.setSpacing(20) self.setMinimumWidth(412) self.setMaximumWidth(412) QApplication.instance().setQuitOnLastWindowClosed(True) # 隐藏任务栏,无边框,置顶等 self.setWindowFlags(self.windowFlags() | Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) # 去掉窗口边框 self.setFrameShape(self.NoFrame) # 背景透明 self.viewport().setAutoFillBackground(False) self.setAttribute(Qt.WA_TranslucentBackground, True) # 不显示滚动条 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # 获取屏幕高宽 rect = QApplication.instance().desktop().availableGeometry(self) self.setMinimumHeight(rect.height()) self.setMaximumHeight(rect.height()) self.move(rect.width() - self.minimumWidth() - 18, 0) def removeItem(self, item): # 删除item w = self.itemWidget(item) self.removeItemWidget(item) item = self.takeItem(self.indexFromItem(item).row()) w.close() w.deleteLater() del item @classmethod def _createInstance(cls): # 创建实例 if not cls._instance: cls._instance = NotificationWindow() cls._instance.show() NotificationIcon.init() @classmethod def info(cls, title, message, callback=None): cls._createInstance() item = QListWidgetItem(cls._instance) w = NotificationItem(title, message, item, cls._instance, ntype=NotificationIcon.Info, callback=callback) w.closed.connect(cls._instance.removeItem) item.setSizeHint(QSize(cls._instance.width() - cls._instance.spacing(), w.height())) cls._instance.setItemWidget(item, w) @classmethod def success(cls, title, message, callback=None): cls._createInstance() item = QListWidgetItem(cls._instance) w = NotificationItem(title, message, item, cls._instance, ntype=NotificationIcon.Success, callback=callback) w.closed.connect(cls._instance.removeItem) item.setSizeHint(QSize(cls._instance.width() - cls._instance.spacing(), w.height())) cls._instance.setItemWidget(item, w) @classmethod def warning(cls, title, message, callback=None): cls._createInstance() item = QListWidgetItem(cls._instance) w = NotificationItem(title, message, item, cls._instance, ntype=NotificationIcon.Warning, callback=callback) w.closed.connect(cls._instance.removeItem) item.setSizeHint(QSize(cls._instance.width() - cls._instance.spacing(), w.height())) cls._instance.setItemWidget(item, w) @classmethod def error(cls, title, message, callback=None): cls._createInstance() item = QListWidgetItem(cls._instance) w = NotificationItem(title, message, item, ntype=NotificationIcon.Error, callback=callback) w.closed.connect(cls._instance.removeItem) width = cls._instance.width() - cls._instance.spacing() item.setSizeHint(QSize(width, w.height())) cls._instance.setItemWidget(item, w) if __name__ == '__main__': import sys import cgitb sys.excepthook = cgitb.Hook(1, None, 5, sys.stderr, 'text') from PyQt5.QtWidgets import QApplication, QPushButton app = QApplication(sys.argv) w = QWidget() layout = QHBoxLayout(w) def callback(): print('回调点击') layout.addWidget(QPushButton( 'Info', w, clicked=lambda: NotificationWindow.info('提示', '这是一条会自动关闭的消息', callback=callback))) layout.addWidget(QPushButton( 'Success', w, clicked=lambda: NotificationWindow.success('提示', '这是一条会自动关闭的消息', callback=callback))) layout.addWidget(QPushButton( 'Warning', w, clicked=lambda: NotificationWindow.warning( '提示', '这是提示文案这是提示文案这是提示文案这是提示文案。', callback=callback))) layout.addWidget(QPushButton( 'Error', w, clicked=lambda: NotificationWindow.error( '提示', '
这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案
', callback=callback))) w.show() # NotificationIcon.init() # ww = NotificationItem('提示', '这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案
', None, # ntype=NotificationIcon.Error) # ww.bgWidget.setVisible(True) # ww.show() sys.exit(app.exec_())