简体   繁体   English

在使用 matplotlib 和 PySide2 运行的应用程序中使用 pdb 调试器时,“事件循环已经在运行”

[英]“event loop is already running” while using the pdb debugger in an application running with matplotlib and PySide2

I have an application in which the user has to click on images using matplotlib.我有一个应用程序,用户必须在其中使用 matplotlib 单击图像。 I want to use the debugger pdb (line 53), however, when I launch the app and click on the button, a message我想使用调试器pdb (第 53 行),但是,当我启动应用程序并单击按钮时,会出现一条消息

QCoreApplication::exec: The event loop is already running

appears and prevents me of using the debugger.出现并阻止我使用调试器。 I suspect that it comes from the following lines but I'm not sure我怀疑它来自以下几行,但我不确定

import matplotlib
try:
    matplotlib.rcParams['backend.qt5'] = 'PySide2'
except (KeyError, Exception):
    pass
matplotlib.use('Qt5Agg')

I did implement those lines thanks to the answer I got from this question I asked previously: Same code with PyQT5/PySide2 runs on MacOS but throws an error on Linux由于我之前问过的这个问题的答案,我确实实现了这些行: 与 PyQT5/PySide2 相同的代码在 MacOS 上运行,但在 Linux 上引发错误

How could I retain the same code structure with backend and being able to use the pdb debugger?如何在后端保留相同的代码结构并能够使用 pdb 调试器?

import sys
import os 
import subprocess
from subprocess import call
import matplotlib
import pdb

try:
    matplotlib.rcParams['backend.qt5'] = 'PySide2'
except (KeyError, Exception):
    pass
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt


from PySide2 import QtGui
from PySide2 import QtCore
from PySide2.QtWidgets import QWidget, QApplication, QPushButton, QFileDialog
from PySide2.QtCore import QFile, QTextStream

os.environ['QT_MAC_WANTS_LAYER'] = '1'


from IPython.core import ultratb
import pdb

sys.excepthook = ultratb.FormattedTB(mode='Verbose', color_scheme='Linux', call_pdb=True)


class GUI(QWidget):
    def __init__(self):
        super(GUI, self).__init__()

        self.initUI()

    def initUI(self):

        height_btn = 40 
        width_btn = 350
        
        button_position_x = 0
        button_position_y = 20 

        button_position_x = button_position_x = 0
        button_position_y = button_position_y + 400
        btn15 = QPushButton('button', self)     
        btn15.clicked.connect(self.Plotfunction)
        btn15.resize(width_btn, height_btn)
        btn15.move(button_position_y, button_position_x)       
        self.show()

    def Plotfunction(self):
        pdb.set_trace()
        print("ok")


def main():

    app = QApplication(sys.argv)
    ex = GUI()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

By default matplotlib will load the binding (PyQt or PySide2) that is already imported, or if there are none loaded then it will try to import them and the first one that manages to import it will use it.默认情况下,matplotlib 将加载已经导入的绑定(PyQt 或 PySide2),或者如果没有加载,那么它将尝试导入它们,第一个设法导入它的将使用它。 Apparently in the case of the OP it is first importing PyQt causing that problem.显然,在 OP 的情况下,它首先导入导致该问题的 PyQt。 The solution is to import PySide2 and then matplotlib so that it prefers PySide2 as the backend binding.解决方案是先导入 PySide2,然后导入 matplotlib,这样它就更喜欢 PySide2 作为后端绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM