PyQt5 popup notification

This is not a question, but a solution. Maybe someone will find this helpful. I spent a lot of time looking for an example pyqt popup notification. I found only c ++ code, but could not convert it to PyQt5. So I had to write it myself. It uses transparency animations to show and hide the window.

from PyQt5 import QtCore, QtGui, QtWidgets import sys class PopupWindowClass(QtWidgets.QWidget): popuphidden = QtCore.pyqtSignal() def __init__(self): super(PopupWindowClass, self).__init__() self.setWindowFlags(QtCore.Qt.SplashScreen | QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint) self.setMinimumSize(QtCore.QSize(300, 100)) self.animation = QtCore.QPropertyAnimation(self, b"windowOpacity", self) self.animation.finished.connect(self.hide) self.timer = QtCore.QTimer() self.timer.timeout.connect(self.hideAnimation) self.setupUi() self.setPopupText('  \n   ') def setupUi(self): self.verticalLayout = QtWidgets.QVBoxLayout(self) self.label = QtWidgets.QLabel(self) font = QtGui.QFont() font.setPointSize(14) self.label.setFont(font) self.label.setAlignment(QtCore.Qt.AlignCenter) self.verticalLayout.addWidget(self.label) appearance = self.palette() appearance.setColor(QtGui.QPalette.Normal, QtGui.QPalette.Window, QtGui.QColor("green")) self.setPalette(appearance) def setPopupText(self, text): self.label.setText(text) self.label.adjustSize() def mousePressEvent(self, event): if event.button() == QtCore.Qt.LeftButton: self.dragPosition = event.globalPos() - self.frameGeometry().topLeft() event.accept() def mouseMoveEvent(self, event): if event.buttons() == QtCore.Qt.LeftButton: self.move(event.globalPos() - self.dragPosition) event.accept() def show(self): self.setWindowOpacity(0.0) self.animation.setDuration(1500) self.animation.setStartValue(0.0) self.animation.setEndValue(1.0) QtWidgets.QWidget.show(self) self.animation.start() self.timer.start(5000) def hideAnimation(self): self.timer.stop() self.animation.setDuration(1500) self.animation.setStartValue(1.0) self.animation.setEndValue(0.0) self.animation.start() def hide(self): if self.windowOpacity() == 0: QtWidgets.QWidget.hide(self) self.popuphidden.emit() if __name__ == '__main__': sys.exit() if __name__ == '__main__': def move2RightBottomCorner(win): try: screen_geometry = QtWidgets.QApplication.desktop().availableGeometry() screen_size = (screen_geometry.width(), screen_geometry.height()) win_size = (win.frameSize().width(), win.frameSize().height()) x = screen_size[0] - win_size[0] - 10 y = screen_size[1] - win_size[1] - 10 win.move(x, y) except Exception as e: print (e) app = QtWidgets.QApplication(sys.argv) main_window = PopupWindowClass() main_window.show() move2RightBottomCorner(main_window) sys.exit(app.exec_()) 

Popup example

+7
python animation pyqt5 popup notifications
source share

No one has answered this question yet.

See related questions:

388
Chrome desktop notification example
170
Creating a notification system
2
Kivy widgets behave randomly
one
Paraboloid optimization requiring scaling
one
Problems with PyOpenGL and PyQt5 on OSX
0
Python notification popup that disappears
0
Search PyQt5 widget for popup
0
matplotlib navigation toolbar edit curves and parameters line color automatically changes the problem
0
Python QT multiple GUI and switch between them
-2
Python: AttributeError: object type 'Ui_Form' does not have attribute 'comboBox'

All Articles