# Only needed for access to command line arguments import sys from PyQt5.QtGui import QPalette, QColor from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QApplication # 自定义窗口,继承 QMainWindow def index_changed(s): print(s) class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("My Awesome App") # 标题 layout = QGridLayout() layout.addWidget(Color('red'), 0, 0) layout.addWidget(Color('green'), 0, 1) layout.addWidget(Color('blue'), 1, 0) layout.addWidget(Color('purple'), 1, 1) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) class Color(QWidget): def __init__(self, color, *args, **kwargs): super(Color, self).__init__(*args, **kwargs) self.setAutoFillBackground(True) palette = self.palette() palette.setColor(QPalette.Window, QColor(color)) self.setPalette(palette) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() # 默认下 windows 是隐藏的 # 开启事件循环 app.exec_()