Qt Threads
Verfasst: Montag 24. November 2008, 03:27
Nur eine kleine Spielerei zum Einstieg, vielleicht hilfts ja jemandem
Code: Alles auswählen
from time import sleep
from PyQt4 import QtGui, QtCore
class Main(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setWindowTitle('Fibonacci Generator')
self.layout = QtGui.QVBoxLayout(self)
self.text = QtGui.QTextEdit(self)
self.layout.addWidget(self.text)
self.run_button = QtGui.QPushButton(self)
self.run_button.setText('Run')
self.layout.addWidget(self.run_button)
self.workers = []
self.connect(self.run_button, QtCore.SIGNAL('clicked()'), self.start)
def start(self):
worker = Worker()
self.connect(worker, QtCore.SIGNAL('fib(int, int)'), self.handle)
worker.start()
self.workers.append(worker)
def handle(self, n, thread):
self.text.append('Got {0} by thread-{1}'.format(n, thread))
class Worker(QtCore.QThread):
count = 0
def __init__(self):
QtCore.QThread.__init__(self)
self.__class__.count += 1
self.count = self.__class__.count
def run(self):
x = 0
y = 1
while True:
self.emit(QtCore.SIGNAL('fib(int, int)'), x, self.count)
x, y = y, x + y
sleep(3)
if __name__ == '__main__':
app = QtGui.QApplication([])
main = Main()
main.show()
app.exec_()