简体   繁体   English

如何从PyQt5通知消息中获取消息?

[英]How to get the message from a PyQt5 notification message?

I'd like to write an application using PyQt5 that has various notification messages. 我想使用具有各种通知消息的PyQt5编写一个应用程序。 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. 线程每10秒发送一次信号。 When a signal sent, two types of messages pops up. 发送信号时,会弹出两种消息。 When a user clicks on the message tray_msg_clicked method activates. 当用户单击消息tray_msg_clicked激活tray_msg_clicked方法。 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! 谢谢您的帮助!

The responsible module for show this messages the QBalloonTip class that doesn't have public interface, so unfortunately this is unavailable from python. 用于显示此消息的负责模块向没有公共接口的QBalloonTip类发送消息,因此很遗憾,这无法从python使用。

https://code.woboq.org/qt5/qtbase/src/widgets/util/qsystemtrayicon_p.h.html#QBalloonTip 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. 尝试修改QSystemTrayIcon实例的toolTip属性,或在最后一条消息的文本中引入一个新变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM