How to change QPushButton text and background color

前端 未结 3 486
甜味超标
甜味超标 2020-12-06 10:07

I am using following code to connect QMenu to QPushButton. When button is clicked a pull-down menu with multiple sub-menu\'s items is shown.

<
相关标签:
3条回答
  • 2020-12-06 10:19

    I would add a comment to Trilarions answer, but not enough rep..

    I was able to use his suggestion, without removing borders by

    self.show() 
    self.button.setStyleSheet('background-color: red;')
    

    AFTER doing a .show() on my application. not sure why after works but not before. If anyone can explain that would be great

    0 讨论(0)
  • 2020-12-06 10:26

    For those who still want to change color of button with the instruction

    button.setStyleSheet('QPushButton {background-color: #A3C1DA}')
    

    and not able to do so, just modify the above instruction to

    button.setStyleSheet('QPushButton {background-color: #A3C1DA; border:  none}')
    

    And it will change the button color, so the trick is to remove the border

    0 讨论(0)
  • 2020-12-06 10:37

    Apart from some inconsistencies with your code example setting the background color and text color of a QPushButton works just fine with:

    setStyleSheet('QPushButton {background-color: #A3C1DA; color: red;}')
    

    Example (using PySide):

    from PySide import QtGui
    
    app = QtGui.QApplication([])
    
    button = QtGui.QPushButton()
    button.setStyleSheet('QPushButton {background-color: #A3C1DA; color: red;}')
    button.setText('Press Me')
    menu = QtGui.QMenu()
    menuItem1 = menu.addAction('Menu Item1')
    menuItem2 = menu.addAction('Menu Item2')
    
    button.setMenu(menu)
    button.show()
    
    app.exec_()
    

    results in:

    enter image description here

    0 讨论(0)
提交回复
热议问题