Search code examples
pythonnotificationspyqt5messageballoon

How to get the message from a PyQt5 notification message?


I'd like to write an application using PyQt5 that has various notification messages. Is it possible to get the text from a balloon message when a user clicked on it?

I wrote a sample code that simulate my question. A thread sending signals every 10 seconds. When a signal sent, two types of messages pops up. When a user clicks on the message tray_msg_clicked method activates. But this is where I stuck. I can't identify what was the message.

from PyQt5 import QtCore, QtWidgets
from time import sleep


class Ui_MainWindow(QtWidgets.QMainWindow):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1218, 502)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(827, 500))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.mythread = ExecuteThread()
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.initUI()

    def initUI(self):
        self.tray()
        self.start_thread()

    def start_thread(self):
        self.mythread.start()
        self.mythread.my_signal.connect(self.thread_signal)

    def thread_signal(self, msg):
        if msg == "message1":
            self.tray_icon.showMessage("Message1", "This is message one.",
                                       QtWidgets.QSystemTrayIcon.Information, 5000)
        elif msg == "message2":
            self.tray_icon.showMessage("Message2", "This is message two.",
                                       QtWidgets.QSystemTrayIcon.Information, 5000)

    def tray_msg_clicked(self):
        print("Tray message clicked!")
        tray_msg = ""
        if tray_msg == "This is message one.":
            print("'Message one' code activated.")
            # Some code
        elif tray_msg == "This is message two.":
            print("'Message two' code activated.")
            # Some code

    def tray(self):
        self.tray_icon = QtWidgets.QSystemTrayIcon(self)
        self.tray_icon.messageClicked.connect(self.tray_msg_clicked)
        self.tray_icon.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_ComputerIcon))
        tray_menu = QtWidgets.QMenu()
        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()


class ExecuteThread(QtCore.QThread):
    my_signal = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(ExecuteThread, self).__init__(parent)

    def run(self):
        while True:
            self.my_signal.emit("message1")
            sleep(10)
            self.my_signal.emit("message2")
            sleep(10)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Anyone have any idea or workaround to identify the text from these notification messages? Thank You for your help!


Solution

  • The responsible module for show this messages the QBalloonTip class that doesn't have public interface, so unfortunately this is unavailable from python.

    https://code.woboq.org/qt5/qtbase/src/widgets/util/qsystemtrayicon_p.h.html#QBalloonTip

    Try to modifiy the toolTip attribute of the QSystemTrayIcon instance or introduce a new variable with the text of the last message.