<div dir="ltr">Hello all:<br>I write a small program to display the cpu MHz as follow:<br><br>#!/usr/bin/python<br>#cpu.py<br><br>import sys<br>import time<br>import threading<br>from PyQt4 import QtGui<br><br>class MainWindow(QtGui.QWidget):<br>
def __init__(self):<br> QtGui.QWidget.__init__(self)<br><br> self.setGeometry(300,300,300,250)<br> self.setWindowTitle('cpu info')<br><br> label_0=QtGui.QLabel('CPU0 MHz',self)<br>
label_0.move(100,20)<br><br> label_1=QtGui.QLabel('CPU1 MHz',self)<br> label_1.move(100,120)<br><br> self.text_0=QtGui.QLineEdit(self)<br> self.text_0.move(100,60)<br>
<br> self.text_1=QtGui.QLineEdit(self)<br> self.text_1.move(100,160)<br><br> event=threading.Event()<br> thread = threading.Thread(target=self.repeat,args=(event,1,self.readcpuinfo))<br>
<br> thread.start()<br><br> time.sleep(10)<br> event.set()<br><br><br> def repeat(self,event,every,action):<br> while True:<br> event.wait(every)<br>
if event.isSet():<br> break<br> action()<br> def readcpuinfo(self):<br> f=open('/proc/cpuinfo')<br>
k=0<br> while True:<br> line=f.readline()<br> if len(line)==0:<br> break<br> elif 'cpu MHz' in line:<br>
if k==0:<br> self.text=QtGui.QLineEdit(self)<br> self.text.move(100,60+k*100)<br> line=line[len('cpu MHz:'):]<br>
self.text_0.setText(unicode(line))<br> k=k+1<br> else:<br> line=line[len('cpu MHz:'):]<br>
self.text_1.setText(unicode(line))<br> f.close()<br><br>app=QtGui.QApplication(sys.argv)<br>main=MainWindow()<br>main.show()<br>sys.exit(app.exec_())<br><br><br>when I run this program,I meet with such error<br style="color: rgb(255, 255, 255);">
<span style="background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);">QObject::setParent: Cannot set parent, new parent is in a different thread</span><br><br>But I can see the information in the lineEdit , I will be very appreciated if someone can give me some help.Thank you.<br>
</div>