Python PyQt5 QTextBrowser 中的所有文本变成'hyperactive'

Python PyQt5 All text in QTextBrowser becomes 'hyperactive'

我在 QTextBrowser 中遇到文本问题。我有一个类似的代码:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.textBox = QTextBrowser(centralWidget)

        self.textBox.setOpenExternalLinks(True)

        self.button = QPushButton(centralWidget)
        self.button.setText("PUSH")
        self.button.clicked.connect(self.pressed)

        self.grid = QGridLayout(centralWidget)
        self.grid.addWidget(self.button)
        self.grid.addWidget(self.textBox)

    def pressed(self):
        id = 49309034
        url_name = "test_link"
        link = '<a href = https://whosebug.com/questions/{0}> {1} </a>'.format(id, url_name)
        dict = {'Key': 'Value', link: 'Test link'}
        for key, value in dict.items():
            self.textBox.append('{0}: {1}'.format(key, value))
        self.textBox.append("")


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

所以,当我按下按钮时,我得到:

但是,如果我单击 link 然后再次按下按钮 - 所有文本都变为 'hyperactive':

我想,问题出在'next line'。因为我已经尝试过这样的代码并且它工作正常:

string = ""
for key, value in dict.items():
    string += '{0}: {1}'.format(key, value) + '; '
self.textBox.append(string)

在我点击 URL 并按下按钮之后

你能帮我解决这个问题吗?

在向 QTextBrowser 添加行之前尝试移动光标。 例如,像这样:

self.textBox.moveCursor(QTextCursor.Start)

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.textBox = QTextBrowser(centralWidget)

        self.textBox.setOpenExternalLinks(True)

        self.button = QPushButton(centralWidget)
        self.button.setText("PUSH")
        self.button.clicked.connect(self.pressed)

        self.grid = QGridLayout(centralWidget)
        self.grid.addWidget(self.button)
        self.grid.addWidget(self.textBox)

    def pressed(self):

        self.textBox.moveCursor(QTextCursor.Start)       # <---

        id = 49309034
        url_name = "test_link"
        link = '<a href = https://whosebug.com/questions/{0}> {1} </a>'.format(id, url_name)
        dict = {'Key': 'Value', link: 'Test link'}
        for key, value in dict.items():
            self.textBox.append('{0}: {1}'.format(key, value))
        self.textBox.append("")


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