0

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")))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dennis
  • 269
  • 1
  • 13
  • Which version of PyQt is this? – FHTMitchell Jun 19 '18 at 11:07
  • 3.3 or 3.4 I think – Dennis Jun 19 '18 at 11:09
  • 1
    Have a look at Qt doc. [QTextEdit](http://doc.qt.io/qt-5/qtextedit.html#details). You can set rich text (which allows coloring). There is an extra page for [Supported HTML Subset](http://doc.qt.io/qt-5/richtext-html-subset.html). – Scheff's Cat Jun 19 '18 at 11:09
  • QT designer 4.2.1 – Dennis Jun 19 '18 at 11:10
  • `ui.title`? I'm not sure whether this can be used in window title bars. (I'm rather sure you cannot but this may depend on the Window Manager in quest.) – Scheff's Cat Jun 19 '18 at 11:11
  • https://stackoverflow.com/questions/1464591/how-to-create-a-bold-red-text-label-in-qt – frogatto Jun 19 '18 at 11:12
  • The line appears in a label, not the title bar at top – Dennis Jun 19 '18 at 11:13
  • `QLabel` supports rich text. You can use [`QLabel::setTextFormat`](http://doc.qt.io/qt-5/qlabel.html#textFormat-prop) to make sure the label will interpret the string as rich text. See the supported subset [here](http://doc.qt.io/qt-5/richtext-html-subset.html). – thuga Jun 19 '18 at 11:31

1 Answers1

1

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

S. Nick
  • 12,879
  • 8
  • 25
  • 33