QCoreApplication::exec: 当同时使用 qdarkstyle 和 python 调试器 pdb 时,事件循环已经 运行

QCoreApplication::exec: The event loop is already running while using qdarkstyle and the python debugger pdb at the same time

在此应用程序中(运行 在 MacOS 11.2.3 上),同时使用 qdarkstyle 和 python 调试器 pdb,如果我们单击按钮,将出现以下消息:

QCoreApplication::exec: The event loop is already running

如果注释掉第 6 行和第 65 行,消息就会消失

你知道如何解决这个问题吗?是否有另一个包有深色背景

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

import qdarkstyle

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



import matplotlib
import matplotlib.pyplot as plt

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



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


from IPython.core import ultratb

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)
    app.setStyleSheet(qdarkstyle.load_stylesheet())
    ex = GUI()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

默认情况下,qdarkstyle 将加载 PyQt5,因此 PySide2 和 PyQt5 之间存在冲突,从而导致意外行为。解决方案是明确指向 qdarkstyle 将使用的后端:

app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api="pyside2"))