【问题标题】:How do I change the color of the text in a menu bar item in PyQt5?如何更改 PyQt5 菜单栏项中文本的颜色?
【发布时间】:2019-06-28 16:10:05
【问题描述】:

假设我有一个带有菜单栏的 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】:

    试试看:

    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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2011-03-31
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      相关资源
      最近更新 更多