【问题标题】:PyQt5 QLabel() string contains < > [duplicate]PyQt5 QLabel() 字符串包含 < > [重复]
【发布时间】:2022-01-20 14:02:42
【问题描述】:

我在 PYQT5 中有一个字符串,我将其加载到包含小于和大于符号的 QLabel() 中。输出省略了符号和它们之间的所有内容。有没有办法按原样输出这个字符串?

Output


from PyQt5.QtWidgets import *
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 400, 400)

        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        
        self.label = QLabel()
        
        self.vLayout = QVBoxLayout()
        self.vLayout.addWidget(self.label)
        self.centralWidget.setLayout(self.vLayout)

        myString = 'Supply Issue < 20 VDC when Speed > 100 MPH'
        num = '10'

        self.label.setText(f'<font color = "#E4551F">{num}:</font> {myString}')


app = QApplication(sys.argv)
myWin = MainWindow()
myWin.show()
app.exec()

'''

【问题讨论】:

  • 您在哪个小部件上设置文本?我不认为 QListWidget 本身有文本。如果 HTML 是问题,您应该能够在插值字符串上使用 html.escape 将其解释为普通字符而不是 markdown
  • 您是否使用自定义委托来显示格式化文本?另外,请提供minimal reproducible example
  • 对不起 - 我对问题出在哪里感到困惑。它在 QLabel 中——我已经解决了这个问题,这样它应该更容易回答。谢谢大家

标签: python html user-interface pyqt5 qlistwidget


【解决方案1】:

QLabels 采用 HTML,因此您可以在插入的值上使用 html.escape

self.label.setText(f'<font color = "#E4551F">{num}:</font> {html.escape(myString)}')

这会将&amp;lt;&amp;gt; 转换为&amp;lt;&amp;gt;。它还将使&amp; 正常工作。

【讨论】:

  • 谢谢!关于这个主题的另一个问题不清楚,没有为我回答,但你已经向我展示了如何正确执行 html.escape - 非常感谢!
猜你喜欢
  • 2012-11-14
  • 2019-09-22
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2020-12-18
  • 2011-04-13
相关资源
最近更新 更多