如何更改 QLabel 的背景?我有很多标签,所以每个标签都需要单独更改。您需要更改的不是整个 QLabel 类的背景,而是单独更改 1. 如何获取级联表中特定标签的类?
import PyQt5
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon
HEIGHT = 750
WIDTH = 1300
class Menu(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.menubar = parent.menuBar()
self.file = self.menubar.addMenu('&Опции')
self.exitAction = QAction(QIcon('cross-exit.ico'), '&Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('Exit application')
self.exitAction.triggered.connect(parent.close)
self.file.addAction(self.exitAction)
def addMenu(self, actionName, text, command, icon, shortcut=None, statusTip=None) :
self.actionName = QAction(QIcon(icon), text, self)
if shortcut:
self.actionName.setShortcut(shortcut)
if statusTip:
self.actionName.setStatusTip(statusTip)
self.actionName.triggered.connect(command)
self.file.insertAction(self.exitAction, self.actionName)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.q = QDesktopWidget().availableGeometry()
self.r_w, self.r_h = self.q.width(), self.q.height()
self.setGeometry(self.r_w/2-WIDTH/2, self.r_h/2-HEIGHT/2, WIDTH, HEIGHT)
self.w = self.size().width()
self.h = self.size().height()
print(self.w, self.h)
self.setFixedSize(1300, 750)
self.setWindowTitle('Lings')
self.setWindowIcon(QIcon('Lings.ico'))
self.label = QtWidgets.QLabel("sdsd", self)
self.labelAnswer = QtWidgets.QLabel("", self)
self.l_w, self.l_h = 500, 100
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.labelAnswer.resize(self.l_w, self.l_h)
self.labelAnswer.move(self.w/2-self.l_w/2, 250)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.resize(self.l_w, self.l_h)
self.label.move(self.w/2-self.l_w/2, 100)
self.label.setFont(QtGui.QFont('Verdana', 24))
self.labelAnswer.setFont(QtGui.QFont('Verdana', 24))
self.label.setStyleSheet(".QLabel{\n" //Нужно поменять фон не всему класу QLabel, а индивидуально 1. Как получить класс определенного лейбла в каскадной таблице?
"background-color: #E0FFFF\n"
"\n"
"}")
self.label.setStyleSheet(".QLabel{\n"
"background-color: #E0FFFF\n"
"\n"
"}")
self.menu = Menu(self)
self.menu.addMenu('action', 'New', lambda: SecondWindow(self), 'test.ico', shortcut="Ctrl+D", statusTip="Creating")
self.show()
class SecondWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent, QtCore.Qt.Window)
self.initUI()
def initUI(self):
self.q = QDesktopWidget().availableGeometry()
self.r_w, self.r_h = self.q.width(), self.q.height()
self.setGeometry(self.r_w/2-WIDTH/2, self.r_h/2-HEIGHT/2, WIDTH, HEIGHT)
self.setFixedSize(1300, 800)
self.setWindowTitle('Lings2')
self.setWindowIcon(QIcon('Lings.ico'))
self.mainLayout = QtWidgets.QVBoxLayout()
self.buttons = []
for i in range(5):
but = QtWidgets.QPushButton('button {}'.format(i), self)
self.mainLayout.addWidget(but)
self.buttons.append(but)
self.setLayout(self.mainLayout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec_())```
在单独的代码中是这样的:
在这样的 qss 中:
您可以像这样加载外部 qss 文件(假设它被称为 style.qss 并且位于正在启动的 py 文件旁边):