使用python库PyQt5更改主界面的颜色

问题描述 投票:1回答:1

如果是Qwidget类型,下面的代码会更改接口的颜色。如果它是Qmainwidow,我可以更改界面颜色吗?谢谢您的帮助

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_())
python pyqt pyqt5 qtstylesheets
1个回答
0
投票

这似乎是正确显示的代码。

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_())
© www.soinside.com 2019 - 2024. All rights reserved.