如何根据选择更改 QLabel 的背景颜色
Posted
技术标签:
【中文标题】如何根据选择更改 QLabel 的背景颜色【英文标题】:How to change the background color of the QLabel based on selection 【发布时间】:2021-05-06 14:03:06 【问题描述】:我需要更改标签的背景颜色。如果我按 Alt+D,则“仪表板”背景颜色变为浅绿色。而如果我按下Alt+F,那么“文件”的背景色变为浅绿色,同时“仪表板”的背景色变为rgb(255,255,150)等等。
我知道这不是正确的方法。所以我需要你的指导
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()
【问题讨论】:
有什么问题? 通过pythonic方式更改背景颜色@eyllanesc 为什么是"this is not a proper way"
?对我来说,这是正常的方式。但是,如果您要将标签保留在列表中,那么您可以尝试使用for
-loop 为列表中的所有元素(甚至已选择)设置背景rgb(255,255,150)
,然后为已选择的元素设置lightgreen
。
【参考方案1】:
我找到了一个更好的解决方案,有没有其他方法可以简化它?
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)")
【讨论】:
以上是关于如何根据选择更改 QLabel 的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章
根据选择更改 UITableViewCell 中按钮的背景颜色
如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色