如何更改在PyQt5菜单栏项目中的文本颜色?

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

比方说,我有一个具有菜单栏的GUI。

mainMenu = self.menuBar()
mainMenu.setStyleSheet("""QMenuBar { background-color: rgb(45, 45, 48); }""") #Set the background color of the menu bar to black.
testMenu = mainMenu.addMenu('Test') # I want to change the color of this text.

test_dropButton = QAction('Test', self)
test_dropButton = setShortcut('Ctrl+T')
test_dropButton.triggered.connect(self.close)
testMenu.addActtion(test_dropButton)#add button to drop down menu.

我怎样才能改变单独的菜单按钮的QAction的文本的颜色?我会做它通过向样式表调用QAction::item rgb(r, g, b)或者是有一个更有效的方法?

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

试试吧:

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


qss = """
QMenuBar {
    background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                      stop:0 lightgray, stop:1 darkgray);
}
QMenuBar::item {
    spacing: 3px;           
    padding: 2px 10px;
    background-color: rgb(210,105,30);
    color: rgb(255,255,255);  
    border-radius: 5px;
}
QMenuBar::item:selected {    
    background-color: rgb(244,164,96);
}
QMenuBar::item:pressed {
    background: rgb(128,0,0);
}

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */  

QMenu {
    background-color: #ABABAB;   
    border: 1px solid black;
    margin: 2px;
}
QMenu::item {
    background-color: transparent;
}
QMenu::item:selected { 
    background-color: #654321;
    color: rgb(255,255,255);
}
"""

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv       
        mainMenu = self.menuBar()
        testMenu = mainMenu.addMenu('mainMenu') 

        test1_dropButton = QAction('Test 1', self)
        testMenu.addAction(test1_dropButton)

        test2_dropButton = QAction('Test 2', self)
        test2_dropButton.triggered.connect(self.displayImage)
        testMenu.addAction(test2_dropButton)

        test_dropButton = QAction('Exit', self)
        test_dropButton.setShortcut('Ctrl+E')
        test_dropButton.triggered.connect(self.close)
        testMenu.addAction(test_dropButton)         
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    def initUI(self):
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        self.layV = QVBoxLayout(central_widget)               

    def displayImage(self):
        pixmap = QPixmap("D:/_Qt/img/pyqt.jpg") 
        lbl    = QLabel(self)
        self.layV.addWidget(lbl)
        lbl.setPixmap(pixmap.scaled(400, 400, Qt.KeepAspectRatio))        


if __name__ == '__main__':
    app = QApplication([])

    app.setStyleSheet(qss)         # <--------------------------------

    ex = Example()
    ex.setWindowTitle('Style Sheet: QMenuBar, QMenu')
    ex.setWindowIcon(QIcon('D:/_Qt/img/py-qt.png')) 
    ex.resize(420,440)
    ex.show()
    sys.exit(app.exec_())

enter image description here

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