Dennis
Dennis

Reputation: 269

Make one word in string a color in QT

I am trying to make one word (filestatus) in a string a certain color. What I have below does not produce an error but it does not work. I am a beginner and am using a Qt UI.

filestatus = "MARRIED, JOINT"

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

Upvotes: 0

Views: 919

Answers (1)

S. Nick
S. Nick

Reputation: 13661

Try it:

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

Upvotes: 1

Related Questions