PyQt5 - Program to get caption of Radio Button

PyQt5 - Program to get caption of Radio Button

To create a PyQt5 program that retrieves the caption (or text) of a selected radio button, you need to set up a group of radio buttons and connect their state to a function that identifies the selected radio button. Below is a step-by-step guide and an example program to demonstrate this.

Step 1: Install PyQt5

If you haven't already installed PyQt5, you can do so using pip:

pip install pyqt5 

Step 2: Import Necessary Modules

Import the required modules from PyQt5:

from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QVBoxLayout, QLabel import sys 

Step 3: Create the Main Window Class

Define a class for your main window, which inherits from QWidget:

class MainWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # Create layout layout = QVBoxLayout() # Create radio buttons self.radio1 = QRadioButton("Option 1") self.radio2 = QRadioButton("Option 2") self.radio3 = QRadioButton("Option 3") # Add radio buttons to layout layout.addWidget(self.radio1) layout.addWidget(self.radio2) layout.addWidget(self.radio3) # Label to display selected option self.label = QLabel("Select an option") layout.addWidget(self.label) # Set the layout for the main window self.setLayout(layout) # Connecting the radio button signals self.radio1.toggled.connect(self.onClicked) self.radio2.toggled.connect(self.onClicked) self.radio3.toggled.connect(self.onClicked) def onClicked(self): radioBtn = self.sender() if radioBtn.isChecked(): self.label.setText(f"Selected Option: {radioBtn.text()}") 

In the initUI method, three radio buttons are created, added to the layout, and connected to the onClicked method. The onClicked method sets the text of the label to the caption of the selected radio button.

Step 4: Run the Application

Create an instance of QApplication and your MainWindow, and start the event loop:

if __name__ == '__main__': app = QApplication(sys.argv) ex = MainWindow() ex.show() sys.exit(app.exec_()) 

Full Program

Here's the complete program put together:

from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QVBoxLayout, QLabel import sys class MainWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.radio1 = QRadioButton("Option 1") self.radio2 = QRadioButton("Option 2") self.radio3 = QRadioButton("Option 3") layout.addWidget(self.radio1) layout.addWidget(self.radio2) layout.addWidget(self.radio3) self.label = QLabel("Select an option") layout.addWidget(self.label) self.setLayout(layout) self.radio1.toggled.connect(self.onClicked) self.radio2.toggled.connect(self.onClicked) self.radio3.toggled.connect(self.onClicked) def onClicked(self): radioBtn = self.sender() if radioBtn.isChecked(): self.label.setText(f"Selected Option: {radioBtn.text()}") if __name__ == '__main__': app = QApplication(sys.argv) ex = MainWindow() ex.show() sys.exit(app.exec_()) 

When you run this program, it will display a window with three radio buttons and a label. Clicking on a radio button will update the label with the text of the selected radio button.


More Tags

exceldatareader itunes fullcalendar-4 django-cache replaceall numpy-ndarray listbox gwt cgrect keras

More Programming Guides

Other Guides

More Programming Examples