Inheritance PyQT从同一文件中的其他类(Qthread)访问UI元素

Inheritance PyQT从同一文件中的其他类(Qthread)访问UI元素,inheritance,pyqt,pyqt5,qthread,qdialog,Inheritance,Pyqt,Pyqt5,Qthread,Qdialog,我也看到过类似的问题,但我的案例没有带来好运。我试图从其他类访问UI元素,但是 我得到以下错误。“工人温度”功能中出现错误 AttributeError: 'rack_temp' object has no attribute 'ui' 我尝试的代码:-main.py from PyQt5.uic.properties import QtWidgets from master.ui_code.fast_charging_ui import Ui_Dialog from PyQt5.QtWidg

我也看到过类似的问题,但我的案例没有带来好运。我试图从其他类访问UI元素,但是 我得到以下错误。“工人温度”功能中出现错误

AttributeError: 'rack_temp' object has no attribute 'ui'
我尝试的代码:-main.py

from PyQt5.uic.properties import QtWidgets
from master.ui_code.fast_charging_ui import Ui_Dialog
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog, QLabel, QMessageBox
from PyQt5.QtCore import QTimer, QObject, pyqtSignal, QRunnable, pyqtSlot, QThreadPool, QByteArray, QEventLoop, QThread

import sys

class MainWindow(QDialog):

    def __init__(self,*args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.ui = Ui_Dialog()    
        self.ui.setupUi(self)
        self.ui.unit_1.setCurrentIndex(0)
        self.threadpool = QThreadPool()
        self.test = rack_temp()
        self.test.start()


    def door_1_check(self):
        print "door check"

class rack_temp(QThread):
    def __init__(self, parent = None):
        QThread.__init__(self, parent)
        super(rack_temp, self).__init__(parent)
        self.threadpool = QThreadPool()
        self.dataCollectionTimer = QTimer()
        self.dataCollectionTimer.moveToThread(self)
        self.dataCollectionTimer.timeout.connect(self.worker_temp)
        self.worker_temp()

    def worker_temp(self):
        print "test "
        self.ui.unit_1.setCurrentIndex(2)

    def run(self):
        self.dataCollectionTimer.start(2000)
        loop = QEventLoop()
        loop.exec_()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
有人能告诉我为什么我不能从其他类继承元素吗?
提前谢谢

我找到了一个解决方案,它可以工作。我们不能直接从不同的线程调用UI元素。我们应该使用signals and slot机制,或者另一种方法是QMetaObject。我尝试了signals and slot方法


尝试将机架温度声明为global@Carlos:这不是一个全局变量问题。
class Rack_Temperature(QtCore.QThread):
    slot1 = QtCore.pyqtSignal(list)
    slot2 = QtCore.pyqtSignal(list) 

    def run(self):

        while True:
            try:
                QtCore.QThread.msleep(3000)

                self.slot1.emit("yourdata")
            except Exception as err:
                print err
class MainWindow(QDialog):

    def __init__(self,*args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.myclass_temp = Rack_Temperature()
        self.myclass_temp.start()
        elf.myclass_temp.slot1.connect(self.test_method)