PyQt - QLabel: set color of text and background

PyQt - QLabel: set color of text and background

To set the color of text and background for a QLabel in PyQt, you can use the setStyleSheet method and provide a style sheet. Here's an example:

from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget from PyQt5.QtGui import QPalette, QColor class MyWindow(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): layout = QVBoxLayout() # Create a QLabel label = QLabel('Hello, PyQt!', self) # Set the text color label.setStyleSheet('color: blue') # Set the background color palette = label.palette() palette.setColor(QPalette.Window, QColor('lightgray')) label.setPalette(palette) layout.addWidget(label) self.setLayout(layout) self.setWindowTitle('QLabel Text and Background Color') self.setGeometry(300, 300, 300, 200) if __name__ == '__main__': app = QApplication([]) window = MyWindow() window.show() app.exec_() 

In this example:

  • The QLabel is created with the text 'Hello, PyQt!'.
  • The setStyleSheet method is used to set the text color to blue ('color: blue').
  • The palette is obtained from the label, and the background color is set using palette.setColor(QPalette.Window, QColor('lightgray')).

Examples

  1. "PyQt QLabel set text color"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Colored Text") label.setStyleSheet("color: blue;") label.show() app.exec_() 
    • Description: Sets the text color of a QLabel to blue.
  2. "PyQt QLabel set background color"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Colored Background") label.setStyleSheet("background-color: yellow;") label.show() app.exec_() 
    • Description: Sets the background color of a QLabel to yellow.
  3. "PyQt QLabel set text and background color"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Colored Text and Background") label.setStyleSheet("color: red; background-color: lightgray;") label.show() app.exec_() 
    • Description: Sets both the text color to red and the background color to light gray in a QLabel.
  4. "PyQt QLabel set text color dynamically"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Dynamic Text Color") label.setStyleSheet("color: black;") # Dynamically change text color label.setStyleSheet("color: green;") label.show() app.exec_() 
    • Description: Sets the text color of a QLabel to green dynamically after it is shown.
  5. "PyQt QLabel set background color dynamically"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Dynamic Background Color") label.setStyleSheet("background-color: white;") # Dynamically change background color label.setStyleSheet("background-color: pink;") label.show() app.exec_() 
    • Description: Sets the background color of a QLabel to pink dynamically after it is shown.
  6. "PyQt QLabel set text and background color dynamically"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("Dynamic Text and Background Color") label.setStyleSheet("color: blue; background-color: lightyellow;") # Dynamically change text and background color label.setStyleSheet("color: darkorange; background-color: lightblue;") label.show() app.exec_() 
    • Description: Dynamically changes both text and background colors of a QLabel after it is shown.
  7. "PyQt QLabel set text color with HTML"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("<font color='purple'>HTML Text Color</font>") label.show() app.exec_() 
    • Description: Sets the text color of a QLabel using HTML format.
  8. "PyQt QLabel set background color with HTML"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel("<html><body style='background-color: lightgreen;'>HTML Background Color</body></html>") label.show() app.exec_() 
    • Description: Sets the background color of a QLabel using HTML format.
  9. "PyQt QLabel set text color on mouse hover"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel from PyQt5.QtCore import Qt app = QApplication([]) class HoverLabel(QLabel): def enterEvent(self, event): self.setStyleSheet("color: red;") def leaveEvent(self, event): self.setStyleSheet("color: black;") label = HoverLabel("Hover Text Color") label.show() app.exec_() 
    • Description: Changes the text color of a QLabel to red on mouse hover and back to black on mouse leave.
  10. "PyQt QLabel set background color on mouse click"

    • Code:
      from PyQt5.QtWidgets import QApplication, QLabel from PyQt5.QtCore import Qt app = QApplication([]) class ClickLabel(QLabel): def mousePressEvent(self, event): self.setStyleSheet("background-color: lightblue;") def mouseReleaseEvent(self, event): self.setStyleSheet("background-color: white;") label = ClickLabel("Click Background Color") label.show() app.exec_() 
    • Description: Changes the background color of a QLabel to light blue on mouse click and back to white on mouse release.

More Tags

numerical-methods ref m2m selector entity-framework text-classification psql mediastore unc accessibility-api

More Programming Questions

More Fitness Calculators

More Everyday Utility Calculators

More Livestock Calculators

More Fitness-Health Calculators