是否有一些功能使文本的一部分成为超链接

问题描述 投票:0回答:1

我正在创建一个应用程序,最后包含“条款和条件”协议复选框。我想要做的是将文本的某些部分作为超链接,这样当用户点击超链接文本时,它应该打开一个新窗口并在该窗口中显示条款和条件。有没有什么方法可以帮助我完成这项任务?

python python-3.x pyqt pyqt5
1个回答
2
投票

您可以通过href使用QLabel,当单击链接时,该href会发出linkActivated信号:

from PyQt5 import QtCore, QtGui, QtWidgets

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    url = "https://github.com/eyllanesc/stackoverflow"
    html = '''<a href="{}">Visit my repo</a>'''.format(url)
    label = QtWidgets.QLabel(html)
    def on_linkActivated(url):
        print(url)
        QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))
    label.linkActivated.connect(on_linkActivated)
    label.show()
    sys.exit(app.exec_())

您可以创建一个QMessabox并将链接包含为HTML,并通过linkActivated信号交叉点击:

from PyQt5 import QtCore, QtGui, QtWidgets

class AboutBox(QtWidgets.QMessageBox):
    open_info = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(AboutBox, self).__init__(parent=parent)

        terms_and_cond = 'myscheme://myapp/terms_and_cond'

        text = "<H1><font color=#fde428>My</font><font color=#002e5b>App</font> Lab</H1>" \
               "<H2>subtitle</H2>" \
               "<p>See <a href=\"{terms_and_cond}\">terms and conditions".format(terms_and_cond=terms_and_cond)
        self.setText(text)
        self.setWindowTitle("About MyApp")
        msg_label = self.findChild(QtWidgets.QLabel, "qt_msgbox_label")
        msg_label.setOpenExternalLinks(False)
        msg_label.linkActivated.connect(self.on_linkActivated)

    def on_linkActivated(self, url):
        if url == "myscheme://myapp/terms_and_cond":
            self.open_info.emit()
        else:
            QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("MyApp")
        self.setCentralWidget(QtWidgets.QTextEdit())

        self._about = AboutBox(self)
        self._about.open_info.connect(self.info)

        help_menu = self.menuBar().addMenu("Help")
        about_action = help_menu.addAction("About me")
        about_action.triggered.connect(self._about.exec_)

    @QtCore.pyqtSlot()
    def info(self):
        msg = QtWidgets.QMessageBox(self)
        msg.setWindowTitle("Terms and Conditions")
        msg.setText("<b>MIT License</b><br>" \
        "Copyright (c) 2019 eyllanesc<br>" \
        "Permission is hereby granted, free of charge, to any person obtaining a copy" \
        "of this software and associated documentation files (the \"Software\"), to deal" \
        "in the Software without restriction, including without limitation the rights" \
        "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" \
        "copies of the Software, and to permit persons to whom the Software is" \
        "furnished to do so, subject to the following conditions<br>" \
        "<br>" \
        "The above copyright notice and this permission notice shall be included in all" \
        "copies or substantial portions of the Software.<br>" \
        "<br>" \
        "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" \
        "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," \
        "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" \
        "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" \
        "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," \
        "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" \
        "SOFTWARE.<br>")
        msg.exec_()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

更新:

如果你想拥有一个可以使用HTML代码的QCheckbox,那就是创建一个QCheckBox + QLabel,如下所示:

from PyQt5 import QtCore, QtGui, QtWidgets

class AboutBox(QtWidgets.QDialog):
    open_license = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(AboutBox, self).__init__(parent)
        self._checkbox = QtWidgets.QCheckBox()
        self._label = QtWidgets.QLabel()
        terms_and_cond = 'myscheme://myapp/license'
        self._label.setText("Make sure to read the <a href=\"{}\">Terms and conditions</a>".format(terms_and_cond))
        lay = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout(self)
        # hlay.setSpacing(0)
        hlay.addWidget(self._checkbox)
        hlay.addWidget(self._label)
        lay.addWidget(QtWidgets.QCheckBox("A"))
        lay.addLayout(hlay)
        lay.addWidget(QtWidgets.QCheckBox("B"))
        self._label.linkActivated.connect(self.open_license)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("MyApp")
        self.setCentralWidget(QtWidgets.QTextEdit())

        self._about = AboutBox(self)
        self._about.open_license.connect(self.info)

        help_menu = self.menuBar().addMenu("Help")
        about_action = help_menu.addAction("About me")
        about_action.triggered.connect(self._about.exec_)

    @QtCore.pyqtSlot()
    def info(self):
        msg = QtWidgets.QMessageBox(self)
        msg.setWindowTitle("Terms and Conditions")
        msg.setText("<b>MIT License</b><br>" \
        "Copyright (c) 2019 eyllanesc<br>" \
        "Permission is hereby granted, free of charge, to any person obtaining a copy" \
        "of this software and associated documentation files (the \"Software\"), to deal" \
        "in the Software without restriction, including without limitation the rights" \
        "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" \
        "copies of the Software, and to permit persons to whom the Software is" \
        "furnished to do so, subject to the following conditions<br>" \
        "<br>" \
        "The above copyright notice and this permission notice shall be included in all" \
        "copies or substantial portions of the Software.<br>" \
        "<br>" \
        "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" \
        "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," \
        "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" \
        "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" \
        "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," \
        "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" \
        "SOFTWARE.<br>")
        msg.exec_()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('fusion')
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.