0

我正在学习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_()  
4

1 回答 1

1

我不明白您为什么将文本放在引号内。通常,如果您有一个字符串并且想要将其转换为QUrluse QUrl::fromUserInput(),也可以使用以下load()方法:

def navigate_bookmark(self):
    action = self.sender()
    url = QUrl.fromUserInput(action.text())
    self.browser.load(url)

另一种策略是将 QUrl 保存在 QAction 中,然后检索它:

def add_bookmark(self):
    bookmark_title = self.browser.page().title()
    bookmark_url = self.browser.url()
    bookmark_select_action = QAction(bookmark_url.toString(), self)
    bookmark_select_action.setData(bookmark_url)
    # or
    # bookmark_select_action.setProperty("url", bookmark_url)
    bookmark_select_action.triggered.connect(self.navigate_bookmark)
    self.bookmark_menu.addAction(bookmark_select_action)
def navigate_bookmark(self):
    action = self.sender()
    url = action.data()
    # or
    # url = action.property("url")
    self.browser.load(url)
于 2021-11-25T04:14:24.107 回答