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.
Here is an example of my 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("myClassA")
self.daemon = True
self.run()
def run(self):
while Startbtn == 1:
print("DONE")
#Doing stuff
time.sleep(0.2)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
And the UI to test it with:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(197, 310)
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
MainWindow.setFont(font)
MainWindow.setStyleSheet("background-color: rgb(252, 252, 252);\n""")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet("")
self.centralwidget.setObjectName("centralwidget")
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("background-color: rgb(65, 218, 78);\n""\n""\n""\n""")
self.B_START.setObjectName("B_START")
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("background-color: rgb(255, 0, 0);")
self.B_STOP.setObjectName("B_STOP")
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("background-color: rgb(255,255,255);\n""\n""border-width: 1px;\n""\n""")
self.B_SET.setObjectName("B_SET")
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("menubar")
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("menulk")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
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("MainWindow", "Panel"))
self.B_START.setText(_translate("MainWindow", "START"))
self.B_STOP.setText(_translate("MainWindow", "STOP"))
self.B_SET.setText(_translate("MainWindow", "SET"))
self.menulk.setTitle(_translate("MainWindow", "TEST"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You just have to put it in the same file and name the ui : "Pannel.py" Is the solution i went with is the good one? If yes, why is main window is still crashing when I press Start.

self.run()toself.start()- eyllanesc