Search code examples
pythonpyqtpyqt5qtstylesheets

Change the color of the main interface with the python library PyQt5


The code below changes the color of the interface if it is Qwidget type. Can I change the interface color if it is Qmainwidow? Thanks for the help

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout)

# class Wind(QMainWindow):  # this what i need
class Wind(QWidget):   
    def __init__(self):      #__init__ method
        super(Wind, self).__init__()
        self.scaleFactor = 0.0

        self.widget = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.widget)

        self.widget.setStyleSheet("""
                .QWidget {
                    background-color: rgb(0, 200, 0);
                    }
                """)

        self.setWindowTitle("first-window")
        self.resize(500, 400)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    imageViewer = Wind()
    imageViewer.show()
    sys.exit(app.exec_())

Solution

  • This seems to be the code properly dispalyed.

    from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout) 
    import sys
    
    #class Wind(QWidget): #Class Name
    
    class Wind(QMainWindow): # Class Name
        def __init__(self):      #__init__ method
            super(Wind, self).__init__()
            self.scaleFactor = 0.0
    
            self.widget = QWidget(self)
            layout = QVBoxLayout(self)
            layout.addWidget(self.widget)
    
            self.widget.setStyleSheet("""
                    .QWidget {
                        background-color: rgb(0, 200, 0);
                        }
                    """)
    
            self.setWindowTitle("first-window")
            self.resize(500, 400)
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        imageViewer = Wind()
        imageViewer.show()
        sys.exit(app.exec_())