我正在学习python并制作书签功能以进行练习。现在我可以将 url 注册到选项卡菜单。当我按下选项卡菜单中的链接时。浏览器上什么都没有显示。以下区域,有人请让我知道出了什么问题。“self.browser.setUrl(QUrl(action_url))”文本本身(action_url)看起来不错,但浏览器不显示。
整个代码是这样的。
import os
import sys
from PyQt5.QtCore import QSize, Qt, QUrl
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import (
QAction,
QApplication,
QDialog,
QDialogButtonBox,
QFileDialog,
QLabel,
QLineEdit,
QMainWindow,
QToolBar,
QVBoxLayout,
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("http://google.com"))
self.setCentralWidget(self.browser)
# tag::navigation1[]
navtb = QToolBar("Navigation")
navtb.setIconSize(QSize(16, 16))
self.addToolBar(navtb)
#bookmark button
bookmark_btn = QAction(QIcon(os.path.join("icons", "smiley.png")), "Bookmark", self)
bookmark_btn.setStatusTip("add to bookmark")
bookmark_btn.triggered.connect(self.add_bookmark)
navtb.addAction(bookmark_btn)
navtb.addSeparator()
# tag::menuBookmark[]
self.bookmark_menu = self.menuBar().addMenu("&Bookmark")
bookmark_select_action = QAction("http://google.com",self)
bookmark_select_action.triggered.connect(self.navigate_bookmark)
self.bookmark_menu.addAction(bookmark_select_action)
self.show()
# tag::bookmark[]
def add_bookmark(self):
bookmark_title = self.browser.page().title()
bookmark_url = self.browser.url().toString()
bookmark_select_action = QAction(bookmark_url,self)
bookmark_select_action.triggered.connect(self.navigate_bookmark)
self.bookmark_menu.addAction(bookmark_select_action)
# tag::navigationBookmark[]
def navigate_bookmark(self):
action = self.sender()
action_url = '"'+action.text()+'"'
self.browser.setUrl(QUrl(action_url)) #this code does not function as I expected
print(action_url)
app = QApplication(sys.argv)
app.setApplicationName("Mozzarella Ashbadger")
app.setOrganizationName("Mozzarella")
app.setOrganizationDomain("mozzarella.org")
window = MainWindow()
app.exec_()