在主类窗口旁边,我定义了新类(test_graph_Data),它将使用matplotlib绘制图形。为了避免报警问题“QPixmap:在GUI线程之外使用pixmaps是不安全的”,我已经定义了新的类test_graph_Data,它会向主类发出信号以绘制图形,但是有一些问题需要发出信号...看看下面的代码:PyQt,QThread,绘制带有QThread问题的matplotlib图形
当我运行代码,我得到了警告:AttributeError:“PyQt4.QtCore.pyqtSignal”对象没有属性'连接'!
import sys, time
from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 120, 90)
self.home()
def home(self):
self.test = QtGui.QPushButton("Test", self)
self.test.clicked.connect(self.test1_function)
self.test.move(10,20)
self.show()
def test1_function(self):
self.get_thread = test_graph_Data()
self.connect(self.get_thread, QtCore.SIGNAL("finished()"),self.done_test1_function)
self.get_thread.start()
def done_test1_function(self):
print 'Graph is displayed!'
class test_graph_Data(QtCore.QThread) :
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def graph_data(self):
start = time.time()
b = [1,0,1,0,1,0,1,1,1,1,0,1,0,1]
plt.ion()
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(b, 'b')
end = time.time()
print end - start
def run(self):
top_post = self.graph_data()
def main():
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
if __name__ == '__main__' :
main()
您不能在主线程(GUI线程)以外的其他线程中显示任何图形。 你想图什么? – eyllanesc
@eyllanesc就是这一点。在初始化test_graph_Data类中的图之后,我想向主线程发送信号以显示图...例如数组b = [1,0,1,0,1,0,1,1,1,1,0 ,1,0,1]就像它在代码中所述。最主要的是该图应该在其他中初始化,而不是在主线程中。 –