如何从一个PyQt5通知消息的消息?

问题描述 投票:0回答:1

我想写使用PyQt5有各种通知消息的应用程序。是否有可能得到,当用户点击它,气球消息的文本?

我写的模仿我的问题的示例代码。一个线程发送信号每10秒。当信号发送的,两种类型的消息的弹出。当用户点击该消息tray_msg_clicked方法激活。但是,这是我坚持。我不能确定究竟是什么消息。

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_())

任何人有任何想法或变通方法,以确定这些通知邮件的文本?谢谢您的帮助!

python notifications pyqt5 message balloon
1个回答
1
投票

对于节目的负责模块这个消息不具有公共接口的QBalloonTip类,所以很不幸,这是蟒蛇不可用。

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

尝试修改QSystemTrayIcon实例的工具提示属性或者与最后一条消息的文本引入新的变量。

© www.soinside.com 2019 - 2024. All rights reserved.