Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt线程属性错误_Python_Multithreading_Pyqt_Qthread - Fatal编程技术网

Python PyQt线程属性错误

Python PyQt线程属性错误,python,multithreading,pyqt,qthread,Python,Multithreading,Pyqt,Qthread,我是PyQt新手,我只想做QThreading。 但我得到了一个错误:AttributeError:“myThread”对象没有属性“ui” 我的代码: from time import sleep from PyQt4.QtGui import * from PyQt4.QtCore import * from takipSistemi import Ui_MainWindow class anaPencere(QMainWindow): def __init__(self):

我是PyQt新手,我只想做QThreading。 但我得到了一个错误:AttributeError:“myThread”对象没有属性“ui”

我的代码:

from time import sleep

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from takipSistemi import Ui_MainWindow



class anaPencere(QMainWindow):
    def __init__(self):
        QWidget.__init__(self)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.thread = myThread()
        self.ui.tableWidget.setItem(0,  0, QTableWidgetItem("hi!"))
        self.thread.start()    

class myThread(QThread):
    def __init__(self,parent=None):
        QThread.__init__(self,parent)
        self.exiting = False

    def __del__(self):
        self.exiting = True
        self.wait()

    def run(self):
        #error
        self.ui.tableWidget.setItem(0 , 0, QTableWidgetItem('hi there!'))

uyg=QApplication([])
pencere=anaPencere()
pencere.show()
uyg.exec_()

如何将ui属性赋予myThread?

anaPencere
myThread
两个类中,只有第一个类分配了属性
self.ui=…
。因为
myThread
没有属性
ui
所以调用
self.ui.tableWidget…
时会出现错误

要解决这个问题,您有几个选项。一种方法是将类
anaPencere
的引用传递给它的线程类:

class anaPencere(QMainWindow):

    def __init__(self):
        QWidget.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.thread = myThread(self)
        self.ui.tableWidget.setItem(0,  0, QTableWidgetItem("hi!"))
        self.thread.start()


class myThread(QThread):

    def __init__(self,parent=None):
        QThread.__init__(self, parent)
        self.exiting = False

    def __del__(self):
        self.exiting = True
        self.wait()

    def run(self):
        self.parent().ui.tableWidget.setItem(0 , 0, QTableWidgetItem('hi there!'))
如果采用这种方法(使用
anaPencere
作为
QThread
父级
),您还可以将
self
作为
myThread
\uuuu init\uuuu
中的第二个参数传递

另一个注意事项是:您几乎总是希望将父对象传递给从Qt的
QObject
派生的每个新创建的对象(请参见此处:)