Carl
Carl

Reputation: 71

Change QLabel color using python code not html?

I have searched for how to change QLabel text however I could not find anything which was not in html. Their was a few things claiming to do this but I could not get it to work really hoping for somthing I can copy and past then work out how it works by playing around with it.

Thank you

This is the code

import sys
from PyQt5 import QtWidgets, QtGui

class Program(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        """expierment"""
        test = QtWidgets.QLabel(self)
        test.setText("I am trying to make this red?")

        self.show()

app = QtWidgets.QApplication(sys.argv)
tradingApp = Program()
sys.exit(app.exec())

Upvotes: 2

Views: 2133

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

Use QPalette:

import sys
from PyQt5 import QtWidgets, QtGui, QtCore

class Program(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        """expierment"""
        test = QtWidgets.QLabel(self)
        pal = test.palette()
        pal.setColor(QtGui.QPalette.WindowText, QtGui.QColor("red"))
        test.setPalette(pal)
        test.setText("I am trying to make this red?")
        self.resize(test.sizeHint())

        self.show()

app = QtWidgets.QApplication(sys.argv)
tradingApp = Program()
sys.exit(app.exec_())

enter image description here

Upvotes: 3

Related Questions