This is a static archive of the Python wiki, which was retired in February 2026 due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

Windows with gradient backgrounds

On the #pyqt channel on Freenode, felipe__ asked if it was possible to change the background colour of a window.

This code shows how it can be done with a fixed gradient. If you are subclassing QWidget or another widget class, it may be worth reimplementing its resizeEvent() method to modify the gradient so that it changes when the window is resized.

   1 import sys
   2 from PyQt4.QtGui import *
   3 
   4 if __name__ == "__main__":
   5 
   6     app = QApplication([])
   7     
   8     w = QWidget()
   9     
  10     p = QPalette()
  11     gradient = QLinearGradient(0, 0, 0, 400)
  12     gradient.setColorAt(0.0, QColor(240, 240, 240))
  13     gradient.setColorAt(1.0, QColor(240, 160, 160))
  14     p.setBrush(QPalette.Window, QBrush(gradient))
  15     w.setPalette(p)
  16     
  17     w.show()
  18     app.exec_()

2026-02-14 16:12