PyQt5 - Change color of Radio button for unchecked state

PyQt5 - Change color of Radio button for unchecked state

To change the color of the radio button for the unchecked state in PyQt5, you can use Qt Style Sheets (QSS), which are similar to CSS for web design.

Below is an example that demonstrates how to change the color of the radio button for its unchecked state using QSS:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton class AppDemo(QWidget): def __init__(self): super().__init__() self.resize(300, 200) layout = QVBoxLayout() # Create radio buttons radio1 = QRadioButton('Option 1') radio2 = QRadioButton('Option 2') radio3 = QRadioButton('Option 3') # Apply the style radio1.setStyleSheet("QRadioButton::indicator:unchecked { background-color: red; }") radio2.setStyleSheet("QRadioButton::indicator:unchecked { background-color: blue; }") radio3.setStyleSheet("QRadioButton::indicator:unchecked { background-color: green; }") # Add widgets to the layout layout.addWidget(radio1) layout.addWidget(radio2) layout.addWidget(radio3) self.setLayout(layout) app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

In this example:

  1. We create three radio buttons with different labels.
  2. We then apply QSS to each radio button separately. The key here is the QRadioButton::indicator:unchecked selector, which targets the indicator part of the radio button when it's in the unchecked state.
  3. We use background-color to set the color of the radio button in its unchecked state.

When you run this code, you will see three radio buttons with different unchecked colors. You can modify the colors in the background-color property to any other valid colors as per your preference.


More Tags

androiddesignsupport splunk format react-table-v6 crc16 scale docker-volume homebrew publish jquery-mobile

More Programming Guides

Other Guides

More Programming Examples