How to change the background color of the QLabel based on selection

Viewed 1198

I need to change the background color of the Label. If I press Alt+D then the "DashBoard" background- color change into the Light green. And if I press Alt+F, then the "File" background color changed to light green, and at the same time, the background color of the "Dashboard" becomes rgb(255,255,150) and so on.

I know this is not a proper way. so I need your guideness

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Label_background(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Label background")
        self.setFixedWidth(800)

        self.lbl_dashboard = QLabel()
        self.lbl_dashboard.setText('<font color="red"><u> D</u></font><font color="black">ash Board < / font > ')
        self.lbl_dashboard.setObjectName("on_lbl_dashboard")
        self.lbl_dashboard.setProperty("type", "1")
        self.lbl_file = QLabel()
        self.lbl_file.setText('<font color="red"><u> F</u></font><font color="black">ile</font>')
        self.lbl_file.setObjectName("on_lbl_file")
        self.lbl_file.setProperty('type', "1")
        self.lbl_Master = QLabel()
        self.lbl_Master.setText('<font color="red"><u> M</u></font><font color="black">aster</font>')
        self.lbl_Master.setObjectName("on_lbl_master")
        self.lbl_Master.setProperty('type', "1")
        self.lbl_Transcation = QLabel()
        self.lbl_Transcation.setText('<font color="red"><u>T</u></font><font color="black">ranscation</font>')
        self.lbl_Transcation.setObjectName("on_lbl_transcation")
        self.lbl_Transcation.setProperty('type', "1")

        self.layout_main_toppannel_1 = QHBoxLayout()
        self.frame_main_toppannel_1 = QFrame()
        self.frame_main_toppannel_1.setStyleSheet("background-color:rgb(255,255,150)")
        self.frame_main_toppannel_1.setProperty('color', "1")
        self.frame_main_toppannel_1.setFixedHeight(30)
        self.layout_main_toppannel_1 = QHBoxLayout(self.frame_main_toppannel_1)
        self.layout_main_toppannel_1.addSpacing(130)
        self.layout_main_toppannel_1.setSpacing(25)
        self.layout_main_toppannel_1.setAlignment(Qt.AlignCenter | Qt.AlignLeft)
        self.layout_main_toppannel_1.addWidget(self.lbl_dashboard)
        self.layout_main_toppannel_1.addWidget(self.lbl_file)
        self.layout_main_toppannel_1.addWidget(self.lbl_Master)
        self.layout_main_toppannel_1.addWidget(self.lbl_Transcation)
        self.layout_main_noticeboard = QHBoxLayout()
        self.layout_main_workscreen = QGridLayout()

        self.layout_main = QVBoxLayout()

        self.layout_main_toppannel_1.setContentsMargins(0, 0, 0, 0)
        self.layout_main.setContentsMargins(0, 0, 0, 0)

        self.layout_main.setSpacing(0)

        self.layout_main.addWidget(self.frame_main_toppannel_1, 2)
        self.layout_main.addLayout(self.layout_main_noticeboard, 3)
        self.layout_main.addLayout(self.layout_main_workscreen, 92)

        self.setLayout(self.layout_main)

    def keyPressEvent(self, event):

        if event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_D:
            self.lbl_dashboard.setStyleSheet("background-color : lightgreen")
        elif event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_F:
            self.lbl_file.setStyleSheet("background-color: lightgreen")
            self.lbl_dashboard.setStyleSheet("background-color: rgb(255,255,150")


def main():
    myapp = QApplication(sys.argv)
    mywindow = Label_background()
    mywindow.show()
    sys.exit(myapp.exec_())


if __name__ == "__main__":
    main()

1 Answers

I dig a somewhat better solution, Is there, any other way to simplify it ?

def keyPressEvent(self, event):
    if event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_D:
        self.backgroundcolor()
        self.lbl_dashboard.setStyleSheet("background-color:lightgreen")
    elif  event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_F:
        self.backgroundcolor()
        self.lbl_file.setStyleSheet("background-color:lightgreen")
    elif event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_M:
        self.backgroundcolor()
        self.lbl_Master.setStyleSheet("background-color:lightgreen")
    elif event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_T:
        self.backgroundcolor()
        self.lbl_Transcation.setStyleSheet("background-color:lightgreen")
    elif event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_R:
        self.backgroundcolor()
        self.lbl_Reports.setStyleSheet("background-color:lightgreen")
    elif event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_O:
        self.backgroundcolor()
        self.lbl_other.setStyleSheet("background-color:lightgreen")
    elif event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_A:
        self.backgroundcolor()
        self.lbl_about.setStyleSheet("background-color:lightgreen")

def backgroundcolor(self):
    self.lbl_dashboard.setStyleSheet("background-color:rgb(255,255,150)")
    self.lbl_file.setStyleSheet("background-color:rgb(255,255,150)")
    self.lbl_Master.setStyleSheet("background-color:rgb(255,255,150)")
    self.lbl_Transcation.setStyleSheet("background-color:rgb(255,255,150)")
    self.lbl_Reports.setStyleSheet("background-color:rgb(255,255,150)")
    self.lbl_other.setStyleSheet("background-color:rgb(255,255,150)")
    self.lbl_about.setStyleSheet("background-color:rgb(255,255,150)")
Related