gpt4 book ai didi

python - 启用插件时pyqt4段错误(核心转储)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:27:38 25 4
gpt4 key购买 nike

我正在使用 PyQt4/QtWebKit 开发一个 python 网络浏览器,我需要启用插件以便它可以呈现 flash 内容。

当我添加这一行时,我得到一个核心转储 QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)

我的系统


Linux rwilson-Aspire-E5-521 3.16.0-47-generic#63~14.04.1-Ubuntu SMP Fri Aug 14 22:56:59 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

PyQt版本


('Qt 版本:', '4.8.6')
('SIP 版本:', '4.15.5')
('PyQt 版本:', '4.10.4')

这是全部代码


#!/usr/bin/env python


from PyQt4 import QtCore, QtGui, QtNetwork, QtWebKit
from PyQt4.QtWebKit import QWebSettings

QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)

try:
import jquery_rc3
except ImportError:
import jquery_rc2


class MainWindow(QtGui.QMainWindow):
def __init__(self, url):
super(MainWindow, self).__init__()

self.progress = 0

fd = QtCore.QFile(":/jquery.min.js")

if fd.open(QtCore.QIODevice.ReadOnly | QtCore.QFile.Text):
self.jQuery = QtCore.QTextStream(fd).readAll()
fd.close()
else:
self.jQuery = ''

QtNetwork.QNetworkProxyFactory.setUseSystemConfiguration(True)

self.view = QtWebKit.QWebView(self)
self.view.load(url)
self.view.loadFinished.connect(self.adjustLocation)
self.view.titleChanged.connect(self.adjustTitle)
self.view.loadProgress.connect(self.setProgress)
self.view.loadFinished.connect(self.finishLoading)

self.locationEdit = QtGui.QLineEdit(self)
self.locationEdit.setSizePolicy(QtGui.QSizePolicy.Expanding,
self.locationEdit.sizePolicy().verticalPolicy())
self.locationEdit.returnPressed.connect(self.changeLocation)

toolBar = self.addToolBar("Navigation")
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Back))
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Forward))
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Reload))
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Stop))
toolBar.addWidget(self.locationEdit)

viewMenu = self.menuBar().addMenu("&View")
viewSourceAction = QtGui.QAction("Page Source", self)
viewSourceAction.triggered.connect(self.viewSource)
viewMenu.addAction(viewSourceAction)

effectMenu = self.menuBar().addMenu("&Effect")
effectMenu.addAction("Highlight all links", self.highlightAllLinks)

self.rotateAction = QtGui.QAction(
self.style().standardIcon(
QtGui.QStyle.SP_FileDialogDetailedView),
"Turn images upside down", self, checkable=True,
toggled=self.rotateImages)
effectMenu.addAction(self.rotateAction)

toolsMenu = self.menuBar().addMenu("&Tools")
toolsMenu.addAction("Remove GIF images", self.removeGifImages)
toolsMenu.addAction("Remove all inline frames",
self.removeInlineFrames)
toolsMenu.addAction("Remove all object elements",
self.removeObjectElements)
toolsMenu.addAction("Remove all embedded elements",
self.removeEmbeddedElements)
self.setCentralWidget(self.view)
self.setUnifiedTitleAndToolBarOnMac(True)

def viewSource(self):
accessManager = self.view.page().networkAccessManager()
request = QtNetwork.QNetworkRequest(self.view.url())
reply = accessManager.get(request)
reply.finished.connect(self.slotSourceDownloaded)

def slotSourceDownloaded(self):
reply = self.sender()
self.textEdit = QtGui.QTextEdit(None)
self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.textEdit.show()
self.textEdit.setPlainText(QtCore.QTextStream(reply).readAll())
self.textEdit.resize(600, 400)
reply.deleteLater()

def adjustLocation(self):
self.locationEdit.setText(self.view.url().toString())

def changeLocation(self):
url = QtCore.QUrl.fromUserInput(self.locationEdit.text())
self.view.load(url)
self.view.setFocus()

def adjustTitle(self):
if 0 < self.progress < 100:
self.setWindowTitle("%s (%s%%)" % (self.view.title(), self.progress))
else:
self.setWindowTitle(self.view.title())

def setProgress(self, p):
self.progress = p
self.adjustTitle()

def finishLoading(self):
self.progress = 100
self.adjustTitle()
self.view.page().mainFrame().evaluateJavaScript(self.jQuery)
self.rotateImages(self.rotateAction.isChecked())

def highlightAllLinks(self):
code = """$('a').each(
function () {
$(this).css('background-color', 'yellow')
}
)"""
self.view.page().mainFrame().evaluateJavaScript(code)

def rotateImages(self, invert):
if invert:
code = """
$('img').each(
function () {
$(this).css('-webkit-transition', '-webkit-transform 2s');
$(this).css('-webkit-transform', 'rotate(180deg)')
}
)"""
else:
code = """
$('img').each(
function () {
$(this).css('-webkit-transition', '-webkit-transform 2s');
$(this).css('-webkit-transform', 'rotate(0deg)')
}
)"""

self.view.page().mainFrame().evaluateJavaScript(code)

def removeGifImages(self):
code = "$('[src*=gif]').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)

def removeInlineFrames(self):
code = "$('iframe').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)

def removeObjectElements(self):
code = "$('object').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)

def removeEmbeddedElements(self):
code = "$('embed').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)


if __name__ == '__main__':

import sys

app = QtGui.QApplication(sys.argv)

if len(sys.argv) > 1:
url = QtCore.QUrl(sys.argv[1])
else:
url = QtCore.QUrl('http://www.google.com/ncr')

browser = MainWindow(url)
browser.show()

sys.exit(app.exec_())

最佳答案

在使用 QWebSettings 之前,您需要有一个 QApplication 的实例。

尝试将有问题的行移动到 MainWindow.__init__

关于python - 启用插件时pyqt4段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32173359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com