PyQt5 – How to change background color of Main window ?
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:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setStyleSheet( "background-color: yellow;" )
self .setWindowTitle( "Color" )
self .setGeometry( 0 , 0 , 400 , 300 )
self .label = QLabel( "Yellow" , self )
self .label.move( 100 , 100 )
self .label.setStyleSheet( "border: 1px solid black;" )
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Example #2:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setStyleSheet( "background-color: cyan;" )
self .setWindowTitle( "Color" )
self .setGeometry( 0 , 0 , 400 , 300 )
self .label = QLabel( "Cyan" , self )
self .label.move( 100 , 100 )
self .label.setStyleSheet( "border: 1px solid black;" )
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Last Updated :
26 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...