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

2024-05-29 03:20:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的代码:pyqt类,我的代码printscreen

我想知道如何将在QThread中收集的值传递给另一个类中的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:我删除了很多不需要的代码,但是如果你需要更多的信息,请告诉我。 谢谢您。在


Tags: path代码importselfinitosdefsys
1条回答
网友
1楼 · 发布于 2024-05-29 03:20:05

如果你想在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。这将使调用立即返回,而不是持续一秒钟(因此,如果需要,可以更频繁地更新进度条)。将其设置为None会导致cpu_percent的第一个调用返回一个无意义的值,但在您的原因中,我认为这不是一个问题。见the documention。在

最后,即使您删除了许多不必要的代码,您的代码示例还不是最小的(例如,import datecs_print不是运行它所必需的,而且我没有这个模块)。我也没有完成(例如“ui/销售_窗口.ui“文件丢失,因此我无法运行它)。请阅读how to make an MCVE上的页面。下一次你会得到更多的帮助,如果你包括一个小程序,我们可以只是复制粘贴执行。在

希望这有帮助

相关问题 更多 >

    热门问题