我從我的代碼中收到此錯誤:
RuntimeWarning: MetaObjectBuilder::addMethod: Invalid method signature provided for "CPU_VALUE"
self.threadclass.CPU_VALUE.connect(SIGNAL('CPU_VALUE'), self.UpdateProgressBar)
from PySide2 import QtWidgets, QtCore
from PySide2.QtCore import SIGNAL, Signal
import main
import sysinfo
class MainUiClass(main.Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainUiClass, self).__init__(parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
self.threadclass.CPU_VALUE.connect(SIGNAL('CPU_VALUE'), self.UpdateProgressBar)
# self.UpdateProgressBar()
def UpdateProgressBar(self):
val = sysinfo.getCPU()
self.progressBar.setValue(val)
class CpuClass(QtCore.QObject):
cpu = Signal()
class ThreadClass(QtCore.QThread):
CPU_VALUE = CpuClass()
cpu = Signal()
def __init__(self, parent=None):
super(ThreadClass, self).__init__(parent)
def run(self):
while 1:
# val = sysinfo.getCPU()
self.CPU_VALUE.emit(SIGNAL('CPU_VALUE'), sysinfo.getCPU())
# print(val)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
a = MainUiClass()
a.show()
app.exec_()
系統資訊檔案:
import psutil
def getCPU():
return psutil.cpu_percent(interval=1)
uj5u.com熱心網友回復:
你的例子有幾個問題。首先,您正在混合舊式和新式信號,這是令人困惑和不必要的。舊式信號永遠不應在新應用程式中使用。其次,您使用的是信號物件 ( CpuSignal),這是不必要的,因為信號可以直接在QThread類上定義。第三,您沒有在信號中發出 cpu 百分比的值,因此進度條將永遠不會更新。最后,您還沒有提供干凈地關閉執行緒的機制。
在下面的演示腳本中,上述所有問題都已修復,我還添加了一個Simulator類來生成 cpu-activity 以進行測驗。單擊“測驗”按鈕將產生大約 20 秒的活動,然后進度條將相應更新:

import psutil
from PySide2 import QtWidgets, QtCore
from PySide2.QtCore import Signal
class MainUiClass(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.progress = QtWidgets.QProgressBar()
self.button = QtWidgets.QPushButton('Test')
self.button.clicked.connect(self.handleButton)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(self.progress)
layout.addWidget(self.button)
self.monitor = CpuMonitor()
self.monitor.cpuPercent.connect(self.progress.setValue)
self.monitor.start()
self.simulator = Simulator()
def handleButton(self):
if not self.simulator.isRunning():
self.simulator.start()
def closeEvent(self, event):
for thread in self.simulator, self.monitor:
thread.stop()
thread.quit()
thread.wait()
class CpuMonitor(QtCore.QThread):
cpuPercent = Signal(int)
def run(self):
self._stopped = False
while not self._stopped:
value = int(psutil.cpu_percent(interval=1))
self.cpuPercent.emit(value)
def stop(self):
self._stopped = True
class Simulator(QtCore.QThread):
def run(self):
self._stopped = False
random = QtCore.QRandomGenerator.system()
timer1 = QtCore.QDeadlineTimer(20000)
while not self._stopped and not timer1.hasExpired():
duration = random.bounded(400, 800)
self.msleep(duration)
timer2 = QtCore.QDeadlineTimer(duration)
while not self._stopped and not timer2.hasExpired():
pass
def stop(self):
self._stopped = True
if __name__ == '__main__':
app = QtWidgets.QApplication(['CPU Monitor'])
a = MainUiClass()
a.show()
app.exec_()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482586.html
標籤:Python 多线程 CPU使用率 pyside2 进度条
上一篇:在執行緒中標記未來物件
