5

当我首先运行 python-pyside2 项目服务器时,它运行良好。
而且该网站也运行良好,但如果我按 F5 btn 在浏览器中刷新。
站点显示错误页面运行时在/

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

下面,使用 pyside2 对代码进行转码:

我不知道我该如何解决?
此致。
谢谢。

4

2 回答 2

6

检查是否已经存在 QApplication 的实例,因为当实例已经在运行并且您正在尝试创建新实例时会发生错误。

把它写在你的主函数中:

if not QtWidgets.QApplication.instance():
    app = QtWidgets.QApplication(sys.argv)
else:
    app = QtWidgets.QApplication.instance()
于 2020-09-02T15:24:55.403 回答
2

对于我的 pyside2 单元测试,以下工作得很好

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

它松散地基于堆栈溢出:unit-and-functional-testing-a-pyside-based-application

于 2019-05-08T01:37:24.767 回答