[PyQt] Threads problems

武艳美 wyiemay at gmail.com
Fri Jul 18 11:09:44 BST 2008


Hello all:
I write a small program to display the cpu MHz as follow:

#!/usr/bin/python
#cpu.py

import sys
import time
import threading
from PyQt4 import QtGui

class MainWindow(QtGui.QWidget):
        def __init__(self):
                QtGui.QWidget.__init__(self)

                self.setGeometry(300,300,300,250)
                self.setWindowTitle('cpu info')

                label_0=QtGui.QLabel('CPU0 MHz',self)
                label_0.move(100,20)

                label_1=QtGui.QLabel('CPU1 MHz',self)
                label_1.move(100,120)

                self.text_0=QtGui.QLineEdit(self)
                self.text_0.move(100,60)

                self.text_1=QtGui.QLineEdit(self)
                self.text_1.move(100,160)

                event=threading.Event()
                thread =
threading.Thread(target=self.repeat,args=(event,1,self.readcpuinfo))

                thread.start()

                time.sleep(10)
                event.set()


        def repeat(self,event,every,action):
                        while True:
                                event.wait(every)
                                if event.isSet():
                                        break
                                action()
 def readcpuinfo(self):
                f=open('/proc/cpuinfo')
                k=0
                while True:
                        line=f.readline()
                        if len(line)==0:
                                        break
                        elif 'cpu MHz' in line:
                                if k==0:
                                        self.text=QtGui.QLineEdit(self)
                                        self.text.move(100,60+k*100)
                                        line=line[len('cpu   MHz:'):]
                                        self.text_0.setText(unicode(line))
                                        k=k+1
                                else:
                                        line=line[len('cpu   MHz:'):]
                                        self.text_1.setText(unicode(line))
                f.close()

app=QtGui.QApplication(sys.argv)
main=MainWindow()
main.show()
sys.exit(app.exec_())


when I run this program,I meet with such error
QObject::setParent: Cannot set parent, new parent is in a different thread

But I can see the information in the lineEdit , I will be very appreciated
if someone can give me some help.Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20080718/2e49773a/attachment.html


More information about the PyQt mailing list