0

My problem here is simple. But I could not solve it by searching. I have a loop function in this code.This function is in the worker class and its name is run.All this window is supposed to run in the main window by pressing the button.My problem is that I can't stop the run function activity.When I close the counter window, the run function does not stop and causes the UI file to crash

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtPrintSupport import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class Ui_counter(object):
    def setupUi(self, counter):
        counter.setObjectName("counter")
        counter.resize(802, 369)
        self.centralwidget = QtWidgets.QWidget(counter)
        self.centralwidget.setObjectName("centralwidget")
        self.startcounting = QtWidgets.QPushButton(self.centralwidget, clicked = lambda : self.stoping())
        self.startcounting.setGeometry(QtCore.QRect(40, 240, 721, 41))

        self.startcounting.setFont(font)
        self.startcounting.setObjectName("startcounting")
        self.counteroutput = QtWidgets.QTextBrowser(self.centralwidget)
        self.counteroutput.setGeometry(QtCore.QRect(40, 41, 721, 190))
        self.counteroutput.setAcceptRichText(True)
        self.counteroutput.setOpenExternalLinks(True)

        self.counteroutput.setFont(font)
        self.counteroutput.setObjectName("counteroutput")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(130, 0, 631, 31))
        
        self.label.setObjectName("label")
        counter.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(counter)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 802, 26))
        self.menubar.setObjectName("menubar")
        counter.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(counter)
        self.statusbar.setObjectName("statusbar")
        counter.setStatusBar(self.statusbar)
        self.retranslateUi(counter)
        QtCore.QMetaObject.connectSlotsByName(counter)
        QTimer.singleShot(1,self.signal)
        self.worker = worker()
    
    
    def set_text(self, mass):
        self.counteroutput.setText(mass)
    
    def stoping(self):
        self.worker.exit()
    
    def retranslateUi(self, counter):
        _translate = QtCore.QCoreApplication.translate
        counter.setWindowTitle(_translate("counter", "counter"))
        self.startcounting.setText(_translate("counter", "stop loop"))
        self.label.setText(_translate("counter", "detail :"))
    
    def signal(self):
        self.worker.start()
        print ("worker started")
        self.worker.data_signal.connect(self.set_text)
    
    


class worker(QThread):
    data_signal = pyqtSignal(str)
    def run(self):
        while  True:
            mass = "doing sth" 
            self.data_signal.emit(mass)
                
     
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_counter()
    window.show()
    sys.exit
  • Instead of writing `while True` on your thread run script, try using a boolean flag such as `while self.isRunning`. This way, you can control wether the while loop is running or not from inside and outside the thread. Whenever you need to close the application, just set this flag to False. – Carl HR Jan 17 '23 at 02:39

0 Answers0