I am right now developing a PyQT5 application an use multithreading to avoid freezing of the GUI. Unfortuneately the Visual Studio Code debugger does not stop on breakpoints inside the executed thread. I tried all suggestions from the following page without fixing the problem. https://github.com/microsoft/ptvsd/issues/428. I think VS Code switched debugger from ptvsd to debugpy so all the suggestions do not hold any more. Maybe somebody has an idea how to fix this issue.
import time
import sys
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QTextEdit, QVBoxLayout, QWidget, QLabel
class Worker(QObject):
sig_msg = pyqtSignal(str) # message to be shown to user
def __init__(self):
super().__init__()
@pyqtSlot()
def work(self):
self.sig_msg.emit('Hello from inside the thread!')
result = 1 + 1
result2 = 1 + 2
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Thread Example")
form_layout = QVBoxLayout()
self.setLayout(form_layout)
self.resize(400, 200)
self.button_start_threads = QPushButton("Start")
self.button_start_threads.clicked.connect(self.start_threads)
self.label = QLabel()
form_layout.addWidget(self.label)
form_layout.addWidget(self.button_start_threads)
QThread.currentThread().setObjectName('main')
self.__threads = None
def start_threads(self):
self.__threads = []
worker = Worker()
thread = QThread()
thread.setObjectName('thread')
self.__threads.append((thread, worker)) # need to store worker too otherwise will be gc'd
worker.moveToThread(thread)
worker.sig_msg.connect(self.label.setText)
thread.started.connect(worker.work)
thread.start()
if __name__ == "__main__":
app = QApplication([])
form = MyWidget()
form.show()
sys.exit(app.exec_())
You can reproduce the error by setting a breakpoint at self.sig_msg.emit('Hello from inside the thrad!') in my case the debugger does not stop at this position. I use VS Code Version 1.65.2. The code is taken from the post mentioned above.