Skip to content

Instantly share code, notes, and snippets.

@jazzycamel
Last active September 28, 2024 08:06
Show Gist options
  • Save jazzycamel/8abd37bf2d60cce6e01d to your computer and use it in GitHub Desktop.
Save jazzycamel/8abd37bf2d60cce6e01d to your computer and use it in GitHub Desktop.

Revisions

  1. jazzycamel revised this gist Jul 12, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion threads1.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Copyright (c) <year> Rob Kent (jazzycamel)
    # Copyright (c) 2016 Rob Kent (jazzycamel)
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    # and associated documentation files (the "Software"), to deal in the Software without restriction,
  2. jazzycamel revised this gist Jul 12, 2024. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions threads1.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,19 @@
    # Copyright (c) <year> Rob Kent (jazzycamel)
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    # and associated documentation files (the "Software"), to deal in the Software without restriction,
    # including without limitation the rights to use, copy, modify, merge, publish, distribute,
    # sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    #
    # The above copyright notice and this permission notice shall be included in all copies or
    # substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
    # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from itertools import count, islice
  3. jazzycamel revised this gist Feb 11, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion threads1.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    from itertools import count, islice

    class Threaded(QObject):
    heartbeat=pyqtSignal()
    result=pyqtSignal(int)

    def __init__(self, parent=None, **kwargs):
  4. jazzycamel revised this gist Feb 11, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions threads1.py
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ def __init__(self, parent=None, **kwargs):
    self.requestPrime.connect(self._threaded.calculatePrime)
    self._thread.started.connect(self._threaded.start)
    self._threaded.moveToThread(self._thread)
    qApp.aboutToQuit.connect(self._thread.quit)
    self._thread.start()

    l=QVBoxLayout(self)
  5. jazzycamel created this gist Feb 11, 2016.
    66 changes: 66 additions & 0 deletions threads1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from itertools import count, islice

    class Threaded(QObject):
    heartbeat=pyqtSignal()
    result=pyqtSignal(int)

    def __init__(self, parent=None, **kwargs):
    super().__init__(parent, **kwargs)

    @pyqtSlot()
    def start(self): print("Thread started")

    @pyqtSlot(int)
    def calculatePrime(self, n):
    primes=(n for n in count(2) if all(n % d for d in range(2, n)))
    self.result.emit(list(islice(primes, 0, n))[-1])

    class GUI(QWidget):
    requestPrime=pyqtSignal(int)

    def __init__(self, parent=None, **kwargs):
    super().__init__(parent, **kwargs)

    self._thread=QThread()
    self._threaded=Threaded(result=self.displayPrime)
    self.requestPrime.connect(self._threaded.calculatePrime)
    self._thread.started.connect(self._threaded.start)
    self._threaded.moveToThread(self._thread)
    self._thread.start()

    l=QVBoxLayout(self)
    self._iterationLE=QLineEdit(self, placeholderText="Iteration (n)")
    l.addWidget(self._iterationLE)
    self._requestBtn=QPushButton(
    "Calculate Prime", self, clicked=self.primeRequested)
    l.addWidget(self._requestBtn)
    self._busy=QProgressBar(self)
    l.addWidget(self._busy)
    self._resultLbl=QLabel("Result:", self)
    l.addWidget(self._resultLbl)

    @pyqtSlot()
    def primeRequested(self):
    try: n=int(self._iterationLE.text())
    except: return
    self.requestPrime.emit(n)
    self._busy.setRange(0,0)
    self._iterationLE.setEnabled(False)
    self._requestBtn.setEnabled(False)

    @pyqtSlot(int)
    def displayPrime(self, prime):
    self._resultLbl.setText("Result: {}".format(prime))
    self._busy.setRange(0,100)
    self._iterationLE.setEnabled(True)
    self._requestBtn.setEnabled(True)

    if __name__=="__main__":
    from sys import exit, argv

    a=QApplication(argv)
    g=GUI()
    g.show()
    exit(a.exec_())