How to change text and background color of QPushButton

I am using the following code to connect QMenu to QPushButton . When a button is pressed, a drop-down menu with several submenu items is displayed.

 button=QPushButton() button.setText("Press Me") font=QtGui.QFont() button.setFont(font) button.setSizePolicy(ToolButtonSizePolicy) button.setPopupMode(QtGui.QToolButton.InstantPopup) menu=QtGui.QMenu() button.setMenu(menu) menuItem1=menu.addAction('Menu Item1') menuItem2=menu.addAction('Menu Item2') 

Now, depending on the conditions, I would like to customize the display of QPushButton by providing it with text and background color. The next line of code (which should change the background color) does not affect the QPushButton connected to QMenu.

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

I would like to know how to change the background color of QPushButton as well as the color of the button text.

+13
python qt pyqt qss qtstylesheets
source share
2 answers

Besides some inconsistencies with your sample code, the background color and text color of QPushButton great job of:

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

leads to:

enter image description here

+21
source share

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

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

and unable to do this, just change the above statement to

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

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

0
source share

All Articles