在QT中将字符串中的一个单词作为颜色

时间:2018-06-19 11:04:59

标签: python pyqt

我试图在字符串中创建一个单词(filestatus)某种颜色。我下面的内容不会产生错误,但它不起作用。我是初学者,正在使用Qt UI。

filestatus = "MARRIED, JOINT"

self.ui.title.setText(str("My Tax Info Based on a Filing Status of:  " + filestatus.format("color=blue")))

1 个答案:

答案 0 :(得分:0)

尝试一下:

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


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

        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)

        pngdir = 'Ok.png';
        filestatus = "MARRIED, JOINT"
        label = QLabel("""
                       <img src={} width=250><hr width=200 style='margin: 15px 0'>
                       My Tax Info Based on a Filing Status of: <b style="color: #0000FF;">{}</b> 
                       """.format(pngdir, filestatus));  

        self.layout = QHBoxLayout(self.centralWidget)
        self.layout.addWidget(label)


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

enter image description here