how do you make it so that the background color and foreground color of a qlabel are different?

Note that when you add the ‘black_background’ Qlabel, you are just creating a new label that will be placed over the ‘dark_red_text’, so this one gets hidden. A possible solution is to set the background color on the window and the text color on the ‘dark_red_text’ Qlabel. The ‘dark_red_text’ will, by default, inherit the background color of the parent widget, which in this case is ‘window’.

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
app = QApplication(sys.argv)
window = QMainWindow()
window.setFixedSize(800, 200)
window.setStyleSheet("background-color: black")
dark_red_text = QLabel(parent=window, text="Dark red text.")
dark_red_text.setFont(QFont("Times New Roman", 40))
dark_red_text.setStyleSheet("color: red")
dark_red_text.move(260, 65)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top