PyQt5 QWebEngineView 不显示任何内容
PyQt5 QWebEngineView not Displaying Anything
所以我正在使用 PyQt5 和 QtWebEngineView 构建一个简单的 Web 浏览器,它工作正常,但是当我在地址栏中键入内容并按回车键时,当前页面发生变化但输入的网址没有加载,屏幕仍然空白。
它也不适用于 https://
"""A Simple Web Browser Written in Python and PyQt5"""
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QLineEdit
class PyChromeWindow(QMainWindow):
DEFAULT_SEARCH_ENGINE = QUrl("https://www.google.com")
def __init__(self):
super(PyChromeWindow, self).__init__()
self.browser = QWebEngineView(self)
self.browser.setUrl(self.DEFAULT_SEARCH_ENGINE)
# ToolBar
self.browser_tool_bar = QToolBar()
self.addToolBar(self.browser_tool_bar)
# Back Action
self.back_btn = QAction(QIcon('./resources/back_arrow16px.png'), 'Back')
self.back_btn.triggered.connect(self.browser.back)
self.browser_tool_bar.addAction(self.back_btn)
# Forward Action
self.forward_action = QAction(QIcon('./resources/forward_arrow16px.png'), 'Forward')
self.forward_action.triggered.connect(self.browser.forward)
self.browser_tool_bar.addAction(self.forward_action)
# Refresh Action
self.refresh_action = QAction(QIcon('./resources/refresh_icon16px.png'), 'Refresh')
self.refresh_action.triggered.connect(self.browser.reload)
self.browser_tool_bar.addAction(self.refresh_action)
# Home Action
self.home_action = QAction(QIcon('./resources/home_icon16px.png'), 'Home')
self.home_action.triggered.connect(lambda: self.browser.setUrl(self.DEFAULT_SEARCH_ENGINE))
self.browser_tool_bar.addAction(self.home_action)
# Address Bar
self.address_bar = QLineEdit()
self.address_bar.returnPressed.connect(self.navigate_to_url)
self.browser_tool_bar.addWidget(self.address_bar)
self.setCentralWidget(self.browser)
self.showMaximized()
def navigate_to_url(self):
"""Navigate to a specific URL"""
url = QUrl(self.address_bar.text())
self.browser.load(url)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName("PyChrome")
window = PyChromeWindow()
app.exec_()
"www.google.com" 不是 QWebEngineView 的有效 url,在这种情况下,您必须使用 QUrl::fromUserInput()
推导出有效的 url.
url = QUrl.fromUserInput(self.address_bar.text())
所以我正在使用 PyQt5 和 QtWebEngineView 构建一个简单的 Web 浏览器,它工作正常,但是当我在地址栏中键入内容并按回车键时,当前页面发生变化但输入的网址没有加载,屏幕仍然空白。
它也不适用于 https://
"""A Simple Web Browser Written in Python and PyQt5"""
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QLineEdit
class PyChromeWindow(QMainWindow):
DEFAULT_SEARCH_ENGINE = QUrl("https://www.google.com")
def __init__(self):
super(PyChromeWindow, self).__init__()
self.browser = QWebEngineView(self)
self.browser.setUrl(self.DEFAULT_SEARCH_ENGINE)
# ToolBar
self.browser_tool_bar = QToolBar()
self.addToolBar(self.browser_tool_bar)
# Back Action
self.back_btn = QAction(QIcon('./resources/back_arrow16px.png'), 'Back')
self.back_btn.triggered.connect(self.browser.back)
self.browser_tool_bar.addAction(self.back_btn)
# Forward Action
self.forward_action = QAction(QIcon('./resources/forward_arrow16px.png'), 'Forward')
self.forward_action.triggered.connect(self.browser.forward)
self.browser_tool_bar.addAction(self.forward_action)
# Refresh Action
self.refresh_action = QAction(QIcon('./resources/refresh_icon16px.png'), 'Refresh')
self.refresh_action.triggered.connect(self.browser.reload)
self.browser_tool_bar.addAction(self.refresh_action)
# Home Action
self.home_action = QAction(QIcon('./resources/home_icon16px.png'), 'Home')
self.home_action.triggered.connect(lambda: self.browser.setUrl(self.DEFAULT_SEARCH_ENGINE))
self.browser_tool_bar.addAction(self.home_action)
# Address Bar
self.address_bar = QLineEdit()
self.address_bar.returnPressed.connect(self.navigate_to_url)
self.browser_tool_bar.addWidget(self.address_bar)
self.setCentralWidget(self.browser)
self.showMaximized()
def navigate_to_url(self):
"""Navigate to a specific URL"""
url = QUrl(self.address_bar.text())
self.browser.load(url)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName("PyChrome")
window = PyChromeWindow()
app.exec_()
"www.google.com" 不是 QWebEngineView 的有效 url,在这种情况下,您必须使用 QUrl::fromUserInput()
推导出有效的 url.
url = QUrl.fromUserInput(self.address_bar.text())