Skip to content

Instantly share code, notes, and snippets.

@jazzycamel
Created February 11, 2016 11:09
Show Gist options
  • Save jazzycamel/06b85f2dbad38a640a11 to your computer and use it in GitHub Desktop.
Save jazzycamel/06b85f2dbad38a640a11 to your computer and use it in GitHub Desktop.
Simple example of threaded loop
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from itertools import count, islice
from time import sleep
class Threaded(QObject):
count=pyqtSignal(int)
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
self._count=0
self._looping=False
@pyqtSlot()
def start(self): print("Thread started")
@pyqtSlot()
def loop(self):
self._count=0
self._looping=True
while self._looping:
self._count+=1
self.count.emit(self._count)
qApp.processEvents()
sleep(1.)
@pyqtSlot()
def stopLoop(self):
self._looping=False
class GUI(QWidget):
startLoop=pyqtSignal()
stopLoop=pyqtSignal()
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
self._thread=QThread()
self._threaded=Threaded(count=self.displayCount)
self._thread.started.connect(self._threaded.start)
self.startLoop.connect(self._threaded.loop)
self.stopLoop.connect(self._threaded.stopLoop)
self._threaded.moveToThread(self._thread)
qApp.aboutToQuit.connect(self._thread.quit)
self._thread.start()
l=QVBoxLayout(self)
l.addWidget(QPushButton("Start", self, clicked=self.startLoop))
l.addWidget(QPushButton("Stop", self, clicked=self.stopLoop))
self._countLbl=QLabel("Count:", self)
l.addWidget(self._countLbl)
@pyqtSlot(int)
def displayCount(self, count):
self._countLbl.setText("Count: {}".format(count))
if __name__=="__main__":
from sys import exit, argv
a=QApplication(argv)
g=GUI()
g.show()
exit(a.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment