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

Issue

In this application (running on MacOS 11.2.3), using qdarkstyle and the python debugger pdb at the same time, if we click on the button, the following message appears:

QCoreApplication::exec: The event loop is already running

the message disappears if you comment out line 6 and line 65

Do you have any idea how to fix the problem? Is there another package to have a dark background

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()


Solution

qdarkstyle by default will load PyQt5 so there is a conflict between PySide2 and PyQt5 causing that unexpected behavior. The solution is to explicitly point to the backend that qdarkstyle will use:

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


Answered By - eyllanesc
Answer Checked By - David Goodson (Python.Easybugfix Volunteer)

Related Posts

Post a Comment