PyQt5,如何让每个物体的颜色不同?

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

我想制作一个具有三个滚动区域的窗口,并更改它们的颜色

class Ui_MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUi()

    def setupUi(self):

        self.setWindowTitle("Main Window")
        self.setGeometry(100, 100, 1024, 768)
        self.setStyleSheet("background-color: #19191A;")
    
    ...
    scroll_area_1 = QScrollArea(self)
        scroll_area_1.setGeometry(120, 100, 300, 600)
        scroll_area_1.setWidgetResizable(True)
        scroll_content_1 = QWidget()
        scroll_area_1.setWidget(scroll_content_1)
        scroll_area_1.setStyleSheet("background-color: #201F22;"
                                    "border-width: 0px;"
                                    "border-radius: 15px;")
        grid_1 = QGridLayout(scroll_content_1)

但是这个滚动区域的颜色没有改变,区域的颜色跟随窗口的颜色。 如何更改滚动区域的颜色?

python user-interface pyqt
1个回答
0
投票

要更改

QscrollArea
的颜色,请设置滚动区域视口的样式表:

from PyQt5.QtWidgets import QMainWindow, QWidget, QScrollArea, QGridLayout
from PyQt5.QtCore import Qt
class Ui_MainWindow(QMainWindow)"
    def __init__(self):
        super().__init__()
        self.setupUi()
    def setupUi(self):
        self.setWindowTitle("Main Window")
        self.setGeometry(100, 100, 1024, 768)
        self.setStyleSheet("background-color: #19191A;")
        scroll_area_1 = QScrollArea(self)
        scroll_area_1.setGeometry(120, 100, 300, 600)
        scroll_area_1.setWidgetResizable(True)
        scroll_area_1.viewport().setStyleSheet("background-color: #201F22;")
        scroll_content_1 = QWidget()
        scroll_area_1.setWidget(scroll_content_1)
        scroll_area_1.setStyleSheet("border-width: 0px; border-radius: 15px;")
        grid_1 = QGridLayout(scroll_content_1)

对其他滚动区域重复此操作。

希望对你有帮助😊

© www.soinside.com 2019 - 2024. All rights reserved.