如何使用 pyqt4 和 python 将值从一个 class 发送到另一个

how to send a value from one class to another using pyqt4 and python

我在构建我的应用程序时遇到了一些问题,我有一个用 pyqt4 和 Python 制作的 GUI,我使用 QThread 每 2 秒检查一次 cpu 加载,我想显示它在进度条上。我的 GUI 在一个 class 中,而我的 Qthread 在另一个中。

这是我的代码:pyqt classes,我的代码打印屏幕

我想知道如何将我在 QThread 中收集的值传递到我另一个 Class 中的 Qobjects。

import sys,os,module,config_read,time,threading,datecs_print,mysql.connector as mariadb,commandList
import psutil,logging
from PyQt4 import QtGui, uic ,QtSql,QtCore
from PyQt4.QtCore import QThread, SIGNAL
import resources
import webbrowser
sys.stderr = open("errlog.txt", "w")

class systemValues(QThread):
    def __init__(self):
        QThread.__init__(self)

    def __del__(self):
        self.wait()

    def cpuRunValue(self):
        while (1):
            for x in range(2):
                p = psutil.cpu_percent(1,False)
                return p

    def cpuProgressBarUpdate(self):
        while(1):
            # MyWindow.setcpuBarValue(val=77)
            MyWindow.cpuBar.setValue(value=77)

    def run(self):
        # self.cpuRunValue()
        print(self.cpuRunValue())
       # self.cpuProgressBarUpdate()

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        super(MyWindow, self).__init__()
        file_path = os.path.abspath("ui/sales_window.ui")
        uic.loadUi(file_path, self)
        self.myThread = systemValues()
        self.myThread.start()

    def setcpuBarValue(self):
        threading.Thread(target=self.cpuBar.setValue(systemValues.cpuRunValue())).start()

这是我的代码,没有错误。我只是无法将我从 cpuRunValue() 获得的值从 MyWindow 转移到 QprogressBar。我对此不是很有经验。

PS:我删除了很多不必要的代码,但如果您需要更多信息,请告诉我。 谢谢。

如果你想在Qt中使用线程,那么你必须很好地理解它们。例如先阅读 this page。当然不要将 QThread 与 Python 标准库中的 threading 包混用。

如果我认为您可能对编程还很陌生,希望您不会被冒犯。我建议您在没有更多经验之前不要使用线程(一般情况下和在 Qt 中)。线程很难正确使用,即使对于经验丰富的程序员也是如此,因此只应在必要时使用。出于这个原因,关于线程的 Qt 文档甚至包括 section about alternatives.

您想每 2 秒执行一个函数,因此在您的情况下,最好的解决方案是使用 QTimer 并将其 timeout 信号连接到更新进度条的插槽。像这样:

#!/usr/bin/env python

import sys
import psutil
from PyQt5 import QtCore, QtWidgets

class MyWidget(QtWidgets.QWidget):

    def __init__(self, parent=None):

        super().__init__(parent=parent)

        self.layout = QtWidgets.QVBoxLayout()
        self.setLayout(self.layout)

        self.progressBar = QtWidgets.QProgressBar()
        self.layout.addWidget(self.progressBar)

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updateCpu)
        self.timer.start(100) # Every 0.1 seconds

    def updateCpu(self):
        cpu = psutil.cpu_percent(None, False)
        #print("cpu = {}%".format(cpu))
        self.progressBar.setValue(cpu)


def main():
    app = QtWidgets.QApplication(sys.argv)

    win = MyWidget()
    win.show()
    win.raise_()
    app.exec_()

if __name__ == "__main__":
    main()

注意我把cpu_percent的第一个参数改成了None。这使得 return 立即调用而不是持续一秒钟(因此您可以根据需要更频繁地更新进度条)。将其设置为 None 会导致 cpu_percent 第一次调用 return 一个无意义的值,但我认为这不是问题。参见 the documention

最后,即使您删除了很多不必要的代码,您的代码示例还不是 最小化(例如 import datecs_print 对于 运行 它,我没有这个模块)。我也没有 complete(例如 "ui/sales_window.ui" 文件丢失所以我不能 运行 它)。请阅读 how to make an MCVE 上的页面。如果您包含一个我们可以复制-粘贴-执行的小程序,下次您将获得更多帮助。

希望这对您有所帮助