Simple PyQt5 counting GUI I just began learning how to use signals and slots in PyQt5, and so I made a cute (pun intended) little program to display just one button. When the button is pressed a thread is spawned which will count from 0 to 99 and display the current count onto the button. However, when the button is counting, the program will not allow the user to spawn another thread.

I am about to actually use my knowledge for a much heavier task, and I wanted to know if there was an easier way to do what I did. Or, perhaps my way is not very efficient? Thanks in advance!

import threading
import sys
import time

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Application(QMainWindow):
    counter = pyqtSignal(str)
    counting = False

    def __init__(self):
        super(Application, self).__init__()

        self.button = QPushButton()
        self.button.setText('99')
        self.button.clicked.connect(self.startCounting)
        self.counter.connect(self.button.setText)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.button)
        self.frame = QFrame()
        self.frame.setLayout(self.layout)
        self.setCentralWidget(self.frame)

    def startCounting(self):
        if not self.counting:
            self.counting = True
            thread = threading.Thread(target=self.something)
            thread.start()

    def something(self):
        for x in range(100):
            self.counter.emit(str(x))
            time.sleep(.01)
        self.counting = False



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Application()
    window.show()
    sys.exit(app.exec_())
Looks good to me!

super minor nits:

        thread = threading.Thread(target=self.something)
        thread.start()


only uses the thread variable once, so you might as well do:

        threading.Thread(target=self.something).start()


Also, the only thing you use from threading is Thread so you might as well change your import to:

from threading import Thread


and then it can be just:

Thread(target=self.something).start()


...but again, these are super minor things!  It looks good to me; I may have to look at pyQT again :)