The part where webpage should be rendered gets white for a fraction of second and then gets empty
这是我的代码(基本上是https://www.pythonguis.com/examples/python-web-browser/ https://www.pythonguis.com/examples/python-web-browser/):
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow,self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("https://www.google.com"))
self.setCentralWidget(self.browser)
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
这是类似的代码,我用它来从本地文件夹渲染 html(也不起作用 - 相同的症状):
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow,self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'temporary_files', "map.html"))
self.browser.load(QUrl.fromLocalFile(file_path))
self.setCentralWidget(self.browser)
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
PyQt5.15.6、python3.8、操作系统 Ubuntu 22.04 LTS。之前在ubuntu 18.04上可以运行,重装系统后出现问题,尽管我备份并恢复了虚拟环境,所以库应该是相同的。