PyQt5 - Change text of pre-existing Radio button

PyQt5 - Change text of pre-existing Radio button

To change the text of a pre-existing radio button in PyQt5, you can use the setText() method provided by the QRadioButton class.

Here's a simple example that demonstrates this:

  1. We'll create a window with a QRadioButton and a QPushButton.
  2. When the push button is clicked, the text of the radio button will change.
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton, QPushButton class AppDemo(QWidget): def __init__(self): super().__init__() self.layout = QVBoxLayout(self) # Create a radio button self.radio_btn = QRadioButton("Initial Text", self) self.layout.addWidget(self.radio_btn) # Create a push button to change the radio button's text self.change_text_btn = QPushButton("Change Text", self) self.change_text_btn.clicked.connect(self.change_radio_text) self.layout.addWidget(self.change_text_btn) def change_radio_text(self): """Change the text of the radio button.""" self.radio_btn.setText("New Text") app = QApplication(sys.argv) window = AppDemo() window.show() sys.exit(app.exec_()) 

In this example, when you click the "Change Text" button, the radio button's text will change from "Initial Text" to "New Text" due to the setText() method.


More Tags

git-checkout browser-testing string-interpolation react-native-text android-optionsmenu utc android-studio-import apk android-backup-service azure-application-gateway

More Programming Guides

Other Guides

More Programming Examples