我知道如何完全改变小部件的背景颜色,但是否有可能以某种方式部分改变背景颜色?
比如上半身的颜色,如下图。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.central_widget.setAttribute(Qt.WA_StyledBackground, True)
self.central_widget.setStyleSheet('background-color: yellow')
self.layout_main_window = QVBoxLayout()
self.central_widget.setLayout(self.layout_main_window)
self.layout_main_window.addWidget(QLabel('Строка 1'))
self.layout_main_window.addWidget(QLabel('Строка 2'))
self.layout_main_window.addWidget(QLabel('Строка 3'))
self.layout_main_window.addWidget(QLabel('Строка 4'))
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
现在像这样:
你需要这样的东西:
UPD: 我们谈论的是小部件,而不是整个窗口,窗口中可以有几个这样的彩色方块,我需要用两种颜色绘制每个方块:




