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

假装没事ソ 提交于 2019-12-12 12:16:32

问题


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.


回答1:


For my pyside2 unit test the following worked fairly well

import PySide2
import unittest

class Test_qapplication(unittest.TestCase):

    def setUp(self):
        super(Test_qapplication, self).setUp()
        if isinstance(PySide2.QtGui.qApp, type(None)):
            self.app = PySide2.QtWidgets.QApplication([])
        else:
            self.app = PySide2.QtGui.qApp


    def tearDown(self):
        del self.app
        return super(Test_qapplication, self).tearDown()

it was loosely based on stack overflow : unit-and-functional-testing-a-pyside-based-application



来源:https://stackoverflow.com/questions/50005373/runtimeerror-please-destroy-the-qapplication-singleton-before-creating-a-new-qa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!