Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance

When i run python-pyside2 project server first, then it works well.
And the site also work well, but if i press F5 btn to refresh in browser.
Site shows error page Runtime at/

import sys

from urllib.request import urlopen  
from bs4 import BeautifulSoup 

from PySide2.QtGui import *  
from PySide2.QtCore import *  
from PySide2.QtWebKitWidgets import *  
from PySide2.QtWidgets import QApplication 

class dynamic_render(QWebPage):

    def __init__(self, url):
        self.frame = None
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)
        self.loadFinished.connect(self._loadFinished)  
        self.mainFrame().load(QUrl(url))
        QTimer.singleShot(0, self.sendKbdEvent)
        QTimer.singleShot(100, app.quit)
        self.app.exec_()

    def _loadFinished(self, result):  
        self.frame = self.mainFrame()  
        self.app.quit()
        self.app = None

Below, scaping code using pyside2:

I don't know how can i fix it?
Best regards.
Thanks.

like image 472
Jayson Reyes Avatar asked Apr 24 '18 15:04

Jayson Reyes


1 Answers

Check if already an instance of QApplication is present or not as the error occurs when an instance is already running and you are trying to create a new one.

Write this in your main function:

if not QtWidgets.QApplication.instance():
    app = QtWidgets.QApplication(sys.argv)
else:
    app = QtWidgets.QApplication.instance()
like image 140
Prachi Poddar Avatar answered Nov 15 '22 04:11

Prachi Poddar