【问题标题】:How to show popup message in Pyqt5 using scheduler如何使用调度程序在 Pyqt5 中显示弹出消息
【发布时间】:2021-09-09 09:27:34
【问题描述】:

我正在开发一个 pyqt5 Trayicon 应用程序,需要添加一个更新功能,我使用 python apscheduler 创建了一个调度程序,该调度程序在后台运行并检查更新。我面临的问题是当更新可用时,我需要显示一个弹出窗口以征得用户同意安装更新,但是每当调用弹出功能时,控制台中都会显示此消息并且应用程序崩溃或停止工作。

NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future.

我的代码sn-p供参考:

class SystemTrayIcon(QtWidgets.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
    ....
    
    def show_popup(self,message):
        msgBox = QMessageBox()
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setText(message)
        msgBox.setWindowTitle("QMessageBox Example")
        msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        msgBox.buttonClicked.connect(self.msgButtonClick)

        returnValue = msgBox.exec()
        if returnValue == QMessageBox.Ok:
            return True
        return False

    def startUpdateScheduler(self):
        scheduler = BackgroundScheduler()
        scheduler.add_job(checkUpdate, 'interval', seconds=5)
        scheduler.start()
    
    def checkUpdate(self):
        if(updatesAvailable):
           installUpdates(self.show_popup("DeviceHub updates are available\n Do you want to install?")

【问题讨论】:

    标签: python macos pyqt5 python-3.8 apscheduler


    【解决方案1】:

    问题是 BackgroundScheduler 在辅助线程中执行回调,并且该线程中的 OP 尝试更新 Qt 禁止的 GUI。相反,您应该使用 QtScheduler:

    from PyQt5 import QtWidgets
    from apscheduler.schedulers.qt import QtScheduler
    
    def startUpdateScheduler(self):
        scheduler = QtScheduler()
        scheduler.add_job(self.checkUpdate, 'interval', seconds=5)
        scheduler.start()
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多