PyQt5 – Change state of Radio Button

In this article we will see how to change the state of radio button. Although with the help of setChecked method we can we make the radio button checked but when we use this method on the checked radio button there will be no change in the state of radio button.

In order to change the state of radio button when push button is pressed we have to do the following –

1. Create a radio button
2. Create a push button
3. Connect a method to push button such that when push button get pressed method get called
4. In the calling method with the help of nextCheckState method change the state of radio button.

Below is the implement.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for widgets
    def UiComponents(self):
  
        # creating a radio button
        self.radio_button = QRadioButton(self)
  
        # setting geometry of radio button
        self.radio_button.setGeometry(200, 150, 120, 40)
  
        # setting text to radio button
        self.radio_button.setText("Radio Button")
  
        # creating push button
        push = QPushButton("Press", self)
  
        # setting geometry of push button
        push.setGeometry(200, 200, 100, 40)
  
        # connect method to push button
        push.clicked.connect(self.method)
  
    def method(self):
  
        # changing the state of radio button
        self.radio_button.nextCheckState()
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

chevron_right


output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.