如何在 Visual Studio Code 中调试 PyQt5 线程?
我现在正在开发PYQT5应用程序的使用多线程以避免GUI冻结。不幸的是,Visual Studio代码调试器不会停止执行线程中的断点。我在未解决问题的情况下尝试了以下页面中的所有建议。 https://github.com/microsoft/ptvsd/ptvsd/issues/428 。我认为VS代码将调试器从PTVSD切换到Debugpy,因此所有建议都不再存在。也许有人有一个想法如何解决此问题。
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_())
您可以通过在self.sig_msg.emit上设置一个断点('hello the the the the the the the!')来重现错误,但在我的情况下,调试器不会在此位置停止。我使用VS代码版本1.65.2。该代码取自上面提到的帖子。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近在VS代码和PYQT5上遇到了同样的问题。按照,我能够通过导入
debugpy
并添加debugpy.debug_this_thread()
在工作方法中添加debugpy
debugpy </>面对同一问题。
I have recently experienced the same issue with VS Code and PyQt5. Following the advice at https://code.visualstudio.com/docs/python/debugging#_troubleshooting, I was able to hit the breakpoint in your example code by importing
debugpy
and addingdebugpy.debug_this_thread()
in the work method e.g.Hopefully that can help others facing the same issue.
安装了Python扩展名(Microsoft制造的扩展程序)可以选择支持QT。女巫可以进行线程调试。我通过浏览Debugpy源代码找到了此选项。
您可以将“ QT”选项添加到启动.json。
例如:
受支持的选项是“自动”,“ Pyside”,“ Pyside2”,“ Pyqt4”和“ Pyqt5”。目前,它似乎与PYQT6不起作用。
With the Python extension installed (the one made by Microsoft) there is an option to enable support of Qt. Witch enables thread debugging. I found this option by going through debugpy source code.
You can add "qt" option to launch.json.
For example:
Supported options are 'auto', 'pyside', 'pyside2', 'pyqt4' and 'pyqt5'. It does not seem to work with PyQt6 for now.