PyQt5 - Change color of pressed Radio button for unchecked state

PyQt5 - Change color of pressed Radio button for unchecked state

In PyQt5, you can change the appearance of widgets, including QRadioButton, using stylesheets. If you want to change the color of a pressed radio button in its unchecked state, you can use the :pressed pseudo-state in combination with the :unchecked pseudo-state in a stylesheet.

Here's an example of how you can set up a QRadioButton and change its color when pressed in the unchecked state:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton class RadioButtonDemo(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Radio Button Color Change Example") self.setGeometry(100, 100, 300, 200) # Create radio buttons self.radioButton1 = QRadioButton("Option 1") self.radioButton2 = QRadioButton("Option 2") # Set the stylesheet to change color of pressed radio button in unchecked state self.setStyleSheet(""" QRadioButton:unchecked:pressed { background-color: red; # Change this to your desired color } """) # Create a vertical layout and add the radio buttons layout = QVBoxLayout() layout.addWidget(self.radioButton1) layout.addWidget(self.radioButton2) self.setLayout(layout) if __name__ == "__main__": app = QApplication(sys.argv) window = RadioButtonDemo() window.show() sys.exit(app.exec_()) 

In the example above, when you press an unchecked radio button, its background color will change to red (as defined by the stylesheet). You can modify the background-color property in the stylesheet to any color you desire.


More Tags

zend-framework3 internal-server-error datepart invisible nasm styling angular-filters print-preview neo4j syntax

More Programming Guides

Other Guides

More Programming Examples