Thread question

Luca Bertolotti luca72.bertolotti at gmail.com
Tue Jul 22 16:09:49 BST 2025


Hello

I have a thread like :
class Homing(QObject):
    segnale_homing = pyqtSignal(bool, bool, )

    def __init__(self, parent=None):
        super().__init__(parent)
        self.pause_homing = True


    def run(self):
        while self.pause_homing == True:
            print('sleep')
            time.sleep(0.3)
        while self.pause_homing == False:
            print('in the thread')
            variable_1 = self.x5.read_bool('M103')
            variable_2= self.x5.read_bool('M102')

            self.segnale_homing.emit(variable_1, variable_2,)

in the main widget I have in the init

self.read_homing = Homing()
self.read_homing.x5 = self.x5
self.read_homing_thread = QThread()
self.read_homing.moveToThread(self.read_homing_thread)
self.read_homing_thread.started.connect(self.read_homing.run)
self.read_homing.segnale_homing.connect(self.readed_homing)
self.read_homing_thread.start()


then I have a button :

@pyqtSlot()
 def on_pushButton_43_clicked(self):
        self.read_homing.pause_homing = False
        self.x5.write_bool('M8', True)
        self.x5.write_bool('M325', True)

Then I have the function connected to the signal

def readed_homing(self,variable_1,variable_2)
       print(variable_,variable_2)
       self.read_homing.pause_homing = True
        msg = QMessageBox(QMessageBox.Icon.Warning, "variable_1"+
str(variable_1)+             "Variable_2 "+str(variable_2))
                msg.setStyleSheet("QMessageBox{ background-color:
qlineargradient(spread:pad, x1:0.227, y1:0.3125, x2:1, y2:1, stop:0 rgba(0,
0, 0, 255), stop:1 rgba(255, 255, 255, 255));}"
                        "QPushButton{background-color: rgba(0, 0,
0,0%);border: 1px solid; border-radius: 3px;border-color: black; color:
black}"
                        "QLabel{color:white;}")
                msg.exec()

I expect that when I set again the variable self.read_homing.pause_homing =
True the thread continues to run with the "print sleep" as he do at the
first start, but in reality the thread never run after I set the variable
pause_homing to True.

So at the beginning he cycles in the "sleep" than when I press the button
he goes in the thread and give me back the variable value, but after I set
again to True the self.read_homing.pause_homing = True  he stop running

What is wrong??
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20250722/0808e153/attachment.htm>


More information about the PyQt mailing list