为了账号安全,请及时绑定邮箱和手机立即绑定

是否有一些功能可以将部分文本作为超链接

是否有一些功能可以将部分文本作为超链接

跃然一笑 2021-12-16 15:02:09
我正在创建一个最后包含“条款和条件”协议复选框的应用程序。我想要做的是将文本的某些部分作为超链接,这样当用户单击超链接文本时,它应该打开一个新窗口并在该窗口中显示条款和条件。有什么方法可以帮助我完成这项任务吗?
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超13个赞

您可以通过在单击链接时发出 linkActivated 信号的 href 使用 QLabel:


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_())



查看完整回答
反对 回复 2021-12-16
  • 1 回答
  • 0 关注
  • 177 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号