How to set QWidget background color?

How to set QWidget background color?

In PyQt, you can set the background color of a QWidget (such as a window, dialog, or other widget) using the setStyleSheet method or by using a QPalette. Here's how you can do it using both methods:

Method 1: Using setStyleSheet

You can set the background color using CSS-style syntax with the setStyleSheet method. Here's an example:

import sys from PyQt5.QtWidgets import QApplication, QWidget app = QApplication(sys.argv) # Create a QWidget widget = QWidget() widget.setGeometry(100, 100, 400, 200) # Set widget's geometry (x, y, width, height) # Set the background color using setStyleSheet widget.setStyleSheet("background-color: lightblue;") widget.show() sys.exit(app.exec_()) 

In this example, we create a QWidget called widget and set its background color to light blue using the setStyleSheet method.

Method 2: Using QPalette

You can also set the background color using a QPalette:

import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPalette, QColor app = QApplication(sys.argv) # Create a QWidget widget = QWidget() widget.setGeometry(100, 100, 400, 200) # Create a QPalette and set the background color palette = QPalette() palette.setColor(QPalette.Window, QColor(173, 216, 230)) # Light blue color # Apply the palette to the widget widget.setPalette(palette) widget.show() sys.exit(app.exec_()) 

In this example, we create a QWidget called widget, create a QPalette, and set the background color using QColor. Then, we apply the palette to the widget using setPalette.

You can replace the color values with any valid color you desire.

Examples

  1. "QWidget set background color example" Description: Discover how to set the background color of a QWidget in PyQt or PySide. This code snippet demonstrates setting the background color of a QWidget.

    from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setStyleSheet("background-color: lightblue;") # Set background color self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  2. "PyQt5 change QWidget background color" Description: Learn how to change the background color of a QWidget using PyQt5. This code snippet illustrates changing the background color of a QWidget.

    from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setAutoFillBackground(True) p = self.palette() p.setColor(self.backgroundRole(), QColor(255, 128, 0)) # Set background color self.setPalette(p) self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  3. "Set background color of QWidget in PySide2" Description: Understand how to set the background color of a QWidget using PySide2. This code snippet demonstrates setting the background color of a QWidget using PySide2.

    from PySide2.QtWidgets import QWidget, QApplication from PySide2.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setStyleSheet("background-color: lightgreen;") # Set background color self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  4. "PyQt5 QWidget background color change" Description: Learn how to change the background color of a QWidget using PyQt5. This code snippet illustrates changing the background color of a QWidget in PyQt5.

    from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setAutoFillBackground(True) p = self.palette() p.setColor(self.backgroundRole(), QColor(0, 255, 0)) # Set background color self.setPalette(p) self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  5. "PySide2 QWidget set background color example" Description: Discover an example of how to set the background color of a QWidget using PySide2. This code snippet demonstrates setting the background color of a QWidget with PySide2.

    from PySide2.QtWidgets import QWidget, QApplication from PySide2.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setStyleSheet("background-color: lightyellow;") # Set background color self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  6. "Change background color of QWidget in PyQt5" Description: Learn how to change the background color of a QWidget using PyQt5. This code snippet illustrates changing the background color of a QWidget with PyQt5.

    from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setAutoFillBackground(True) p = self.palette() p.setColor(self.backgroundRole(), QColor(255, 0, 255)) # Set background color self.setPalette(p) self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  7. "PySide2 set background color of QWidget" Description: Understand how to set the background color of a QWidget using PySide2. This code snippet demonstrates setting the background color of a QWidget with PySide2.

    from PySide2.QtWidgets import QWidget, QApplication from PySide2.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setStyleSheet("background-color: lightgray;") # Set background color self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  8. "PyQt5 QWidget background color set" Description: Learn how to set the background color of a QWidget using PyQt5. This code snippet illustrates setting the background color of a QWidget in PyQt5.

    from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setAutoFillBackground(True) p = self.palette() p.setColor(self.backgroundRole(), QColor(128, 128, 128)) # Set background color self.setPalette(p) self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  9. "Change QWidget background color in PySide2" Description: Learn how to change the background color of a QWidget using PySide2. This code snippet illustrates changing the background color of a QWidget with PySide2.

    from PySide2.QtWidgets import QWidget, QApplication from PySide2.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setStyleSheet("background-color: lightcyan;") # Set background color self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 
  10. "PyQt5 QWidget set background color" Description: Discover how to set the background color of a QWidget using PyQt5. This code snippet demonstrates setting the background color of a QWidget with PyQt5.

    from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setStyleSheet("background-color: lightcoral;") # Set background color self.setGeometry(100, 100, 300, 200) # Set geometry if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_() 

More Tags

autoit price jenkins-declarative-pipeline height sqlclient pattern-matching angularjs-ng-repeat dom4j cryptography docker-build

More Python Questions

More Tax and Salary Calculators

More Pregnancy Calculators

More Investment Calculators

More Stoichiometry Calculators