本文整理汇总了Python中PyQt5.QtWebEngineWidgets.QWebEngineView.setPage方法的典型用法代码示例。如果您正苦于以下问题:Python QWebEngineView.setPage方法的具体用法?Python QWebEngineView.setPage怎么用?Python QWebEngineView.setPage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWebEngineWidgets.QWebEngineView
的用法示例。
在下文中一共展示了QWebEngineView.setPage方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_to_pdf
# 需要导入模块: from PyQt5.QtWebEngineWidgets import QWebEngineView [as 别名]
# 或者: from PyQt5.QtWebEngineWidgets.QWebEngineView import setPage [as 别名]
def print_to_pdf(html, pdf_file):
from PyQt5.QtCore import QUrl
from PyQt5 import QtGui, QtCore
global qapp
if qapp is None:
qapp = QApplication(['--disable-gpu', '--disable-extension'])
view = QWebEngineView()
page = QWebEnginePage()
view.setPage(page)
def pdf_printed(*args, **kwargs):
page.pdfPrintingFinished.disconnect()
page.loadFinished.disconnect()
qapp.quit()
def page_loaded(*args, **kwargs):
page.pdfPrintingFinished.connect(pdf_printed)
page.printToPdf(
pdf_file,
QtGui.QPageLayout(
QtGui.QPageSize(QtGui.QPageSize.A4), QtGui.QPageLayout.Portrait, QtCore.QMarginsF(25, 25, 25, 25)
)
)
page.loadFinished.connect(page_loaded)
if isinstance(html, bytes):
html = html.decode('utf-8')
view.setHtml(html, QUrl('file://'))
qapp.exec_()
return os.path.basename(pdf_file)
示例2: WebViewPlus
# 需要导入模块: from PyQt5.QtWebEngineWidgets import QWebEngineView [as 别名]
# 或者: from PyQt5.QtWebEngineWidgets.QWebEngineView import setPage [as 别名]
class WebViewPlus(QWebEngineView):
"""
WebView 커스터마이징
- inspector 추가
- jsconsole 로그 추가
- webview에서 document로 이벤트를 발생함.
"""
def __init__(self):
super().__init__()
self.setPage(WebPagePlus())
#Keyboard shortcuts
self.shortcut = {}
#F5 - Page reloading
self.shortcut['F5'] = QShortcut(self)
self.shortcut['F5'].setKey(Qt.Key_F5)
self.shortcut['F5'].activated.connect(self.reload)
#Devtool setup
def debuggingMode(self, port):
#F12 - Development tool
self.shortcut['F12'] = QShortcut(self)
self.shortcut['F12'].setContext(Qt.ApplicationShortcut)
self.shortcut['F12'].setKey(Qt.Key_F12)
self.shortcut['F12'].activated.connect(self._toggleDevTool)
self.devTool = QDialog(self)
self.devTool.setWindowTitle("Development Tool")
self.devTool.resize(950, 400)
self.devView = QWebEngineView()
self.devView.setPage(QWebEnginePage(self.devView))
self.devView.load(QUrl("http://localhost:"+port))
layout = QGridLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.devView)
self.devTool.setLayout(layout)
def _toggleDevTool(self):
"""
F12키를 다시 누르면 "개발자 도구"가 사라짐
"""
self.devTool.setVisible(not self.devTool.isVisible())
示例3: BrowserWindow
# 需要导入模块: from PyQt5.QtWebEngineWidgets import QWebEngineView [as 别名]
# 或者: from PyQt5.QtWebEngineWidgets.QWebEngineView import setPage [as 别名]
#.........这里部分代码省略.........
@pyqtSlot()
def onUrlChanged(self):
if self.hoveredUrl != '':
self.urlLabel.setText('<span style="color: gray">{}</span>'.format(html.escape(self.hoveredUrl)))
else:
self.urlLabel.setText(html.escape(self.url().toDisplayString()))
@pyqtSlot()
def onTitleChanged(self):
title = self.webView.page().title()
if title == '':
self.setWindowTitle('shower')
else:
self.setWindowTitle('shower: ' + title)
def addShortcut(self, key, handler):
shortcut = QShortcut(QKeySequence(key), self)
shortcut.activated.connect(handler)
return shortcut
@pyqtSlot()
def onLoadStarted(self):
self.isLoading = True
self.stopShortcut.setEnabled(True)
self.progressLabel.setText('<span style="color: yellow">[ 0%]</span>')
@pyqtSlot(int)
def onLoadProgress(self, n):
if n == 100:
self.progressLabel.setText('<span style="color: green">[100]</span>'.format(n))
else:
self.progressLabel.setText('<span style="color: yellow">[{:2d}%]</span>'.format(n))
@pyqtSlot()
def onLoadFinished(self):
self.isLoading = False
self.stopShortcut.setEnabled(False)
self.urlChanged.emit()
def initUI(self, windowType):
self.setStyleSheet("""
#cmdLine, #bar, #bar > * { border: 0px; background: #070707; font-family: "Pro Font"; font-size: 10px; color: white; min-height: 17px }
""")
self.setWindowTitle('shower')
self.setAttribute(Qt.WA_DeleteOnClose)
vbox = QVBoxLayout()
self.setLayout(vbox)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.setSpacing(0)
bar = QWidget()
bar.setObjectName('bar')
hbox = QHBoxLayout()
hbox.setContentsMargins(2, 0, 0, 0)
hbox.setSpacing(0)
bar.setLayout(hbox)
vbox.addWidget(bar)
self.urlLabel = QLabel()
self.urlLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Preferred)
self.urlLabel.setTextFormat(Qt.RichText)
hbox.addWidget(self.urlLabel)
hbox.setStretch(0, 1)
self.progressLabel = QLabel()
self.progressLabel.setTextFormat(Qt.RichText)
hbox.addWidget(self.progressLabel)
self.cmdLine = QLineEdit()
self.cmdLine.setObjectName('cmdLine')
vbox.addWidget(self.cmdLine)
self.webView = QWebEngineView()
self.webView.setPage(BrowserPage(self))
vbox.addWidget(self.webView)
self.cmdLine.returnPressed.connect(self.handleCommand)
self.webView.page().linkHovered.connect(self.onLinkHovered)
self.webView.page().urlChanged.connect(self.urlChanged)
self.webView.page().titleChanged.connect(self.onTitleChanged)
self.webView.page().loadProgress.connect(self.onLoadProgress)
self.webView.page().loadStarted.connect(self.onLoadStarted)
self.webView.page().loadFinished.connect(self.onLoadFinished)
self.urlChanged.connect(self.onUrlChanged)
self.addShortcut("Alt+Left", lambda: self.webView.page().triggerAction(QWebEnginePage.Back))
self.addShortcut("Alt+Right", lambda: self.webView.page().triggerAction(QWebEnginePage.Forward))
self.addShortcut("Ctrl+R", lambda: self.webView.page().triggerAction(QWebEnginePage.Reload))
self.addShortcut("Ctrl+Shift+R", lambda: self.webView.page().triggerAction(QWebEnginePage.ReloadAndBypassCache))
self.stopShortcut = self.addShortcut("Esc", lambda: self.webView.page().triggerAction(QWebEnginePage.Stop))
self.addShortcut("Ctrl+W", lambda: self.close())
self.addShortcut("Ctrl+L", lambda: self.beginEnteringCommand(None))
self.addShortcut("Ctrl+K", lambda: self.beginEnteringCommand('? '))
self.addShortcut("Ctrl+/", lambda: self.beginEnteringCommand('/ '))
self.addShortcut("Ctrl+U", lambda: self.toggleSource())