title: Threading a class in Python
body: <p>I'm currently doing a program in python using an UI done in Qt. This UI consists of programming some data and sending them after a click of a button every 200ms. My problem is that if I do this in my main class, the program completely crashes (freeze) so I have to do this in another class or function. After a bit of research, I came across threading, I don't know if it's the right solution for me but it's supposed to run my class in another thread(in the background) until I press the stop button without making my main window crash. The second class needs to have a loop until i press the stop button.</p>
  <p>Here is an example of my code:</p>
  <pre><code>import Panel
  from PyQt5 import QtWidgets, QtCore
  import sys
  from threading import Thread
  import threading
  import time
  
  class MainWindow(QtWidgets.QMainWindow):
        
      def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.ui = Panel.Ui_MainWindow()
          self.ui.setupUi(self)
  
          self.ui.B_START.clicked.connect(self.StartBtnClicked)
          self.ui.B_STOP.clicked.connect(self.StopBtnClicked)
  
          #..
          #Other connect and function
          #..
  
      def StartBtnClicked(self):
          global Startbtn
          Startbtn = 1
          myClassA()
  
      def StopBtnClicked(self):
          global Startbtn
          Startbtn = 0
  
  
  class myClassA(Thread):
      def __init__(self):
          Thread.__init__(self)
          print(&quot;myClassA&quot;)
          self.daemon = True
          self.run()
  
      def run(self):
          while Startbtn == 1:
              print(&quot;DONE&quot;) 
              #Doing stuff
              time.sleep(0.2)
                             
  if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication(sys.argv)
      w = MainWindow()
      w.show()
      sys.exit(app.exec_())
  </code></pre>
  <p>And the UI to test it with:</p>
  <pre><code>from PyQt5 import QtCore, QtGui, QtWidgets
  
  
  class Ui_MainWindow(object):
      def setupUi(self, MainWindow):
          MainWindow.setObjectName(&quot;MainWindow&quot;)
          MainWindow.resize(197, 310)
          font = QtGui.QFont()
          font.setPointSize(11)
          font.setBold(True)
          font.setWeight(75)
          MainWindow.setFont(font)
          MainWindow.setStyleSheet(&quot;background-color: rgb(252, 252, 252);\n&quot;&quot;&quot;)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setStyleSheet(&quot;&quot;)
          self.centralwidget.setObjectName(&quot;centralwidget&quot;)
          self.B_START = QtWidgets.QPushButton(self.centralwidget)
          self.B_START.setGeometry(QtCore.QRect(50, 50, 71, 61))
          font = QtGui.QFont()
          font.setPointSize(11)
          font.setBold(True)
          font.setWeight(75)
          self.B_START.setFont(font)
          self.B_START.setFocusPolicy(QtCore.Qt.ClickFocus)
          self.B_START.setStyleSheet(&quot;background-color: rgb(65, 218, 78);\n&quot;&quot;\n&quot;&quot;\n&quot;&quot;\n&quot;&quot;&quot;)
          self.B_START.setObjectName(&quot;B_START&quot;)
          self.B_STOP = QtWidgets.QPushButton(self.centralwidget)
          self.B_STOP.setGeometry(QtCore.QRect(50, 190, 71, 61))
          font = QtGui.QFont()
          font.setPointSize(11)
          font.setBold(True)
          font.setWeight(75)
          self.B_STOP.setFont(font)
          self.B_STOP.setStyleSheet(&quot;background-color: rgb(255, 0, 0);&quot;)
          self.B_STOP.setObjectName(&quot;B_STOP&quot;)
          self.B_SET = QtWidgets.QPushButton(self.centralwidget)
          self.B_SET.setGeometry(QtCore.QRect(50, 140, 71, 23))
          font = QtGui.QFont()
          font.setPointSize(11)
          font.setBold(True)
          font.setWeight(75)
          self.B_SET.setFont(font)
          self.B_SET.setStyleSheet(&quot;background-color: rgb(255,255,255);\n&quot;&quot;\n&quot;&quot;border-width: 1px;\n&quot;&quot;\n&quot;&quot;&quot;)
          self.B_SET.setObjectName(&quot;B_SET&quot;)
          MainWindow.setCentralWidget(self.centralwidget)
          self.menubar = QtWidgets.QMenuBar(MainWindow)
          self.menubar.setGeometry(QtCore.QRect(0, 0, 197, 26))
          font = QtGui.QFont()
          font.setPointSize(11)
          font.setBold(True)
          font.setWeight(75)
          self.menubar.setFont(font)
          self.menubar.setObjectName(&quot;menubar&quot;)
          self.menulk = QtWidgets.QMenu(self.menubar)
          font = QtGui.QFont()
          font.setPointSize(11)
          font.setBold(True)
          font.setWeight(75)
          self.menulk.setFont(font)
          self.menulk.setObjectName(&quot;menulk&quot;)
          MainWindow.setMenuBar(self.menubar)
          self.statusbar = QtWidgets.QStatusBar(MainWindow)
          self.statusbar.setObjectName(&quot;statusbar&quot;)
          MainWindow.setStatusBar(self.statusbar)
          self.menubar.addAction(self.menulk.menuAction())
  
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate(&quot;MainWindow&quot;, &quot;Panel&quot;))
          self.B_START.setText(_translate(&quot;MainWindow&quot;, &quot;START&quot;))
          self.B_STOP.setText(_translate(&quot;MainWindow&quot;, &quot;STOP&quot;))
          self.B_SET.setText(_translate(&quot;MainWindow&quot;, &quot;SET&quot;))
          self.menulk.setTitle(_translate(&quot;MainWindow&quot;, &quot;TEST&quot;))
  
  
  if __name__ == &quot;__main__&quot;:
      import sys
      app = QtWidgets.QApplication(sys.argv)
      MainWindow = QtWidgets.QMainWindow()
      ui = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
      sys.exit(app.exec_())
  </code></pre>
  <p>You just have to put it in the same file and name the ui : &quot;Pannel.py&quot;
  Is the solution i went with is the good one? If yes, why is main window is still crashing when I press Start.</p>