In this article we will see how to change the state of radio button. Although with the help of setChecked method we can 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.
Python3
# 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 ()) |
output :