Skip to content
Related Articles

Related Articles

Improve Article

PyQt5 – How to change background color of Main window ?

  • Last Updated : 26 Mar, 2020

The first step in creating desktop applications with PyQt is getting a window to show up on your desktop, in this article, we will see how we can change the color of this window. In order to change the color of the main window we use setStylesheet() method.

Syntax : setStyleSheet(“background-color: grey;”)

Argument : It takes string as an argument.

Example #1:




# importing the required libraries
  
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5.QtGui import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # changing the background color to yellow
        self.setStyleSheet("background-color: yellow;")
  
        # set the title
        self.setWindowTitle("Color")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
  
        # creating a label widget
        self.label = QLabel("Yellow", self)
  
        # moving position
        self.label.move(100, 100)
  
        # setting up border
        self.label.setStyleSheet("border: 1px solid black;")
  
  
  
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Output :

 
Example #2:






# importing the required libraries
  
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5.QtGui import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # changing the background color to cyan
        self.setStyleSheet("background-color: cyan;")
  
        # set the title
        self.setWindowTitle("Color")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
  
        # creating a label widget
        self.label = QLabel("Cyan", self)
  
        # moving position
        self.label.move(100, 100)
  
        # setting up border
        self.label.setStyleSheet("border: 1px solid black;")
  
  
  
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :