0

I"m writing a GUI application in PyQt5 but can't figure out (or find) a way to change the background color of a layout (not a single widget). Could anyone please point me any way i could do it? Or if there is no way to do so in PyQt5 - what other API for GUI building has this option?

Any help is highly appreciated.

I've written some GUI applications in PyQt5 but couldn't figure out how to change the background color of a layout. All of them were just gray. Now I need to add some "live" to my application - but still stuck at this obstacle. I tried searching on the web for a solution - I found a way to change the color of single widget (which I already know), but not of a layout.

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import (QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QWidget)
from ver_4.functions.static_functions import *
matplotlib.use('Qt5Agg')


class DashBoard(QWidget):
    def __init__(self):
        super().__init__()

        self.main_v_box = QVBoxLayout()

        self.image_label = QLabel('Image Label')
        self.image_label.setStyleSheet('QLabel {background-color: red; color: white; border:1px solid #191970;}')
        self.set_image_path_btn = QPushButton('Set')
        self.browse_image_path_btn = QPushButton('Browse')

        self.init_ui()
        self.show()

    def init_ui(self):
        self.init_layouts()

    def init_layouts(self):
        self.main_v_box.addWidget(self.image_label)
        self.main_v_box.addStretch()
        button_container = QHBoxLayout()
        button_container.addWidget(self.set_image_path_btn)
        button_container.addWidget(self.browse_image_path_btn)
        self.main_v_box.addLayout(button_container)

        self.setLayout(self.main_v_box)


class MainMenu(QMainWindow):
    def __init__(self):
        super().__init__()

        self.dash_board = DashBoard()

        self.setCentralWidget(self.dash_board)
        self.showMaximized()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_menu = MainMenu()
    sys.exit(app.exec_())

I'd like the background of the main window to be green instead of white.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Michael
  • 2,167
  • 5
  • 23
  • 38
  • 1
    Layouts aren't rendered in any way, so there is no background to set a colour on. You simply need to set the background on the parent widget that ***contains*** the layout. – ekhumoro Jan 06 '19 at 15:16

0 Answers0