【问题标题】:PyQt5 - How to display a clickable hyperlink in QTextBrowserPyQt5 - 如何在 QTextBrowser 中显示可点击的超链接
【发布时间】:2021-06-15 08:05:52
【问题描述】:

我用 PyQt5 创建了一个 GUI。现在我想将超链接添加到QTextBrowser。不幸的是,这些文本不可点击,而是显示为普通文本,我很难找出原因。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextBrowser

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.text_browser = QTextBrowser()
        self.text_browser.setOpenExternalLinks(True)
        self.text_browser.setReadOnly(True)
        self.text_browser.append("<a href=https://google.com/>Google</a>")
        self.text_browser.append("<a href=https://github.com/>Github</a>")

        layout = QVBoxLayout()
        layout.addWidget(self.text_browser)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

没有链接的GUI

【问题讨论】:

    标签: python hyperlink pyqt pyqt5 qtextbrowser


    【解决方案1】:

    与标准网络浏览器相比,Qt 使用的 HTML 解析器是初级的,因此首选更标准的语法。

    虽然这是否是一个错误存在争议,但在 HTML 属性周围使用引号总是更好。

        self.text_browser.append("<a href='https://google.com/'>Google</a>")
        self.text_browser.append("<a href='https://github.com/'>Github</a>")
    

    注意:QTextBrowser已经默认为只读,无需设置。

    【讨论】:

      猜你喜欢
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多