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

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


跃然一笑
浏览 196回答 1
1回答

萧十郎

您可以通过在单击链接时发出 linkActivated 信号的 href 使用 QLabel:from PyQt5 import QtCore, QtGui, QtWidgetsif __name__ == '__main__':&nbsp; &nbsp; import sys&nbsp; &nbsp; app = QtWidgets.QApplication(sys.argv)&nbsp; &nbsp; url = "https://github.com/eyllanesc/stackoverflow"&nbsp; &nbsp; html = '''<a href="{}">Visit my repo</a>'''.format(url)&nbsp; &nbsp; label = QtWidgets.QLabel(html)&nbsp; &nbsp; def on_linkActivated(url):&nbsp; &nbsp; &nbsp; &nbsp; print(url)&nbsp; &nbsp; &nbsp; &nbsp; QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))&nbsp; &nbsp; label.linkActivated.connect(on_linkActivated)&nbsp; &nbsp; label.show()&nbsp; &nbsp; sys.exit(app.exec_())您可以创建一个 QMessabox 并将链接包含为 HTML 并通过 linkActivated 信号与单击的内容相交:from PyQt5 import QtCore, QtGui, QtWidgetsclass AboutBox(QtWidgets.QMessageBox):&nbsp; &nbsp; open_info = QtCore.pyqtSignal()&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(AboutBox, self).__init__(parent=parent)&nbsp; &nbsp; &nbsp; &nbsp; terms_and_cond = 'myscheme://myapp/terms_and_cond'&nbsp; &nbsp; &nbsp; &nbsp; text = "<H1><font color=#fde428>My</font><font color=#002e5b>App</font> Lab</H1>" \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<H2>subtitle</H2>" \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<p>See <a href=\"{terms_and_cond}\">terms and conditions".format(terms_and_cond=terms_and_cond)&nbsp; &nbsp; &nbsp; &nbsp; self.setText(text)&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("About MyApp")&nbsp; &nbsp; &nbsp; &nbsp; msg_label = self.findChild(QtWidgets.QLabel, "qt_msgbox_label")&nbsp; &nbsp; &nbsp; &nbsp; msg_label.setOpenExternalLinks(False)&nbsp; &nbsp; &nbsp; &nbsp; msg_label.linkActivated.connect(self.on_linkActivated)&nbsp; &nbsp; def on_linkActivated(self, url):&nbsp; &nbsp; &nbsp; &nbsp; if url == "myscheme://myapp/terms_and_cond":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.open_info.emit()&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))class MainWindow(QtWidgets.QMainWindow):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(MainWindow, self).__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("MyApp")&nbsp; &nbsp; &nbsp; &nbsp; self.setCentralWidget(QtWidgets.QTextEdit())&nbsp; &nbsp; &nbsp; &nbsp; self._about = AboutBox(self)&nbsp; &nbsp; &nbsp; &nbsp; self._about.open_info.connect(self.info)&nbsp; &nbsp; &nbsp; &nbsp; help_menu = self.menuBar().addMenu("Help")&nbsp; &nbsp; &nbsp; &nbsp; about_action = help_menu.addAction("About me")&nbsp; &nbsp; &nbsp; &nbsp; about_action.triggered.connect(self._about.exec_)&nbsp; &nbsp; @QtCore.pyqtSlot()&nbsp; &nbsp; def info(self):&nbsp; &nbsp; &nbsp; &nbsp; msg = QtWidgets.QMessageBox(self)&nbsp; &nbsp; &nbsp; &nbsp; msg.setWindowTitle("Terms and Conditions")&nbsp; &nbsp; &nbsp; &nbsp; msg.setText("<b>MIT License</b><br>" \&nbsp; &nbsp; &nbsp; &nbsp; "Copyright (c) 2019 eyllanesc<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "Permission is hereby granted, free of charge, to any person obtaining a copy" \&nbsp; &nbsp; &nbsp; &nbsp; "of this software and associated documentation files (the \"Software\"), to deal" \&nbsp; &nbsp; &nbsp; &nbsp; "in the Software without restriction, including without limitation the rights" \&nbsp; &nbsp; &nbsp; &nbsp; "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" \&nbsp; &nbsp; &nbsp; &nbsp; "copies of the Software, and to permit persons to whom the Software is" \&nbsp; &nbsp; &nbsp; &nbsp; "furnished to do so, subject to the following conditions<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "The above copyright notice and this permission notice shall be included in all" \&nbsp; &nbsp; &nbsp; &nbsp; "copies or substantial portions of the Software.<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" \&nbsp; &nbsp; &nbsp; &nbsp; "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," \&nbsp; &nbsp; &nbsp; &nbsp; "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" \&nbsp; &nbsp; &nbsp; &nbsp; "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" \&nbsp; &nbsp; &nbsp; &nbsp; "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," \&nbsp; &nbsp; &nbsp; &nbsp; "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" \&nbsp; &nbsp; &nbsp; &nbsp; "SOFTWARE.<br>")&nbsp; &nbsp; &nbsp; &nbsp; msg.exec_()if __name__ == '__main__':&nbsp; &nbsp; import sys&nbsp; &nbsp; app = QtWidgets.QApplication(sys.argv)&nbsp; &nbsp; w = MainWindow()&nbsp; &nbsp; w.resize(640, 480)&nbsp; &nbsp; w.show()&nbsp; &nbsp; sys.exit(app.exec_())更新:如果你想要一个可以使用 HTML 代码的 QCheckbox,诀窍是创建一个 QCheckBox + QLabel,如下所示:from PyQt5 import QtCore, QtGui, QtWidgetsclass AboutBox(QtWidgets.QDialog):&nbsp; &nbsp; open_license = QtCore.pyqtSignal()&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(AboutBox, self).__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; self._checkbox = QtWidgets.QCheckBox()&nbsp; &nbsp; &nbsp; &nbsp; self._label = QtWidgets.QLabel()&nbsp; &nbsp; &nbsp; &nbsp; terms_and_cond = 'myscheme://myapp/license'&nbsp; &nbsp; &nbsp; &nbsp; self._label.setText("Make sure to read the <a href=\"{}\">Terms and conditions</a>".format(terms_and_cond))&nbsp; &nbsp; &nbsp; &nbsp; lay = QtWidgets.QVBoxLayout(self)&nbsp; &nbsp; &nbsp; &nbsp; hlay = QtWidgets.QHBoxLayout(self)&nbsp; &nbsp; &nbsp; &nbsp; # hlay.setSpacing(0)&nbsp; &nbsp; &nbsp; &nbsp; hlay.addWidget(self._checkbox)&nbsp; &nbsp; &nbsp; &nbsp; hlay.addWidget(self._label)&nbsp; &nbsp; &nbsp; &nbsp; lay.addWidget(QtWidgets.QCheckBox("A"))&nbsp; &nbsp; &nbsp; &nbsp; lay.addLayout(hlay)&nbsp; &nbsp; &nbsp; &nbsp; lay.addWidget(QtWidgets.QCheckBox("B"))&nbsp; &nbsp; &nbsp; &nbsp; self._label.linkActivated.connect(self.open_license)class MainWindow(QtWidgets.QMainWindow):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(MainWindow, self).__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("MyApp")&nbsp; &nbsp; &nbsp; &nbsp; self.setCentralWidget(QtWidgets.QTextEdit())&nbsp; &nbsp; &nbsp; &nbsp; self._about = AboutBox(self)&nbsp; &nbsp; &nbsp; &nbsp; self._about.open_license.connect(self.info)&nbsp; &nbsp; &nbsp; &nbsp; help_menu = self.menuBar().addMenu("Help")&nbsp; &nbsp; &nbsp; &nbsp; about_action = help_menu.addAction("About me")&nbsp; &nbsp; &nbsp; &nbsp; about_action.triggered.connect(self._about.exec_)&nbsp; &nbsp; @QtCore.pyqtSlot()&nbsp; &nbsp; def info(self):&nbsp; &nbsp; &nbsp; &nbsp; msg = QtWidgets.QMessageBox(self)&nbsp; &nbsp; &nbsp; &nbsp; msg.setWindowTitle("Terms and Conditions")&nbsp; &nbsp; &nbsp; &nbsp; msg.setText("<b>MIT License</b><br>" \&nbsp; &nbsp; &nbsp; &nbsp; "Copyright (c) 2019 eyllanesc<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "Permission is hereby granted, free of charge, to any person obtaining a copy" \&nbsp; &nbsp; &nbsp; &nbsp; "of this software and associated documentation files (the \"Software\"), to deal" \&nbsp; &nbsp; &nbsp; &nbsp; "in the Software without restriction, including without limitation the rights" \&nbsp; &nbsp; &nbsp; &nbsp; "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" \&nbsp; &nbsp; &nbsp; &nbsp; "copies of the Software, and to permit persons to whom the Software is" \&nbsp; &nbsp; &nbsp; &nbsp; "furnished to do so, subject to the following conditions<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "The above copyright notice and this permission notice shall be included in all" \&nbsp; &nbsp; &nbsp; &nbsp; "copies or substantial portions of the Software.<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "<br>" \&nbsp; &nbsp; &nbsp; &nbsp; "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" \&nbsp; &nbsp; &nbsp; &nbsp; "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," \&nbsp; &nbsp; &nbsp; &nbsp; "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" \&nbsp; &nbsp; &nbsp; &nbsp; "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" \&nbsp; &nbsp; &nbsp; &nbsp; "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," \&nbsp; &nbsp; &nbsp; &nbsp; "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" \&nbsp; &nbsp; &nbsp; &nbsp; "SOFTWARE.<br>")&nbsp; &nbsp; &nbsp; &nbsp; msg.exec_()if __name__ == '__main__':&nbsp; &nbsp; import sys&nbsp; &nbsp; app = QtWidgets.QApplication(sys.argv)&nbsp; &nbsp; app.setStyle('fusion')&nbsp; &nbsp; w = MainWindow()&nbsp; &nbsp; w.resize(640, 480)&nbsp; &nbsp; w.show()&nbsp; &nbsp; sys.exit(app.exec_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python