如何在 Visual Studio Code 中调试 PyQt5 线程?

发布于 2025-01-21 01:48:15 字数 2039 浏览 0 评论 0 原文

我现在正在开发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。该代码取自上面提到的帖子。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-01-28 01:48:15

我最近在VS代码和PYQT5上遇到了同样的问题。按照,我能够通过导入 debugpy 并添加 debugpy.debug_this_thread()在工作方法中添加 debugpy debugpy </

def work(self):
    debugpy.debug_this_thread()
    self.sig_msg.emit('Hello from inside the thread!')

    result = 1 + 1
    result2 = 1 + 2

>面对同一问题。

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 adding debugpy.debug_this_thread() in the work method e.g.

def work(self):
    debugpy.debug_this_thread()
    self.sig_msg.emit('Hello from inside the thread!')

    result = 1 + 1
    result2 = 1 + 2

Hopefully that can help others facing the same issue.

甜心小果奶 2025-01-28 01:48:15

安装了Python扩展名(Microsoft制造的扩展程序)可以选择支持QT。女巫可以进行线程调试。我通过浏览Debugpy源代码找到了此选项。

您可以将“ QT”选项添加到启动.json。
例如:

{
    "name": "Python Debugger: custom debugpy",
    "type": "debugpy",
    "request": "launch",
    "program": "main.py",
    "console": "integratedTerminal",
    "qt": "pyqt5"
}

受支持的选项是“自动”,“ 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:

{
    "name": "Python Debugger: custom debugpy",
    "type": "debugpy",
    "request": "launch",
    "program": "main.py",
    "console": "integratedTerminal",
    "qt": "pyqt5"
}

Supported options are 'auto', 'pyside', 'pyside2', 'pyqt4' and 'pyqt5'. It does not seem to work with PyQt6 for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文