In this article we will see how we can set and access the name of the radio button. Name is basically used to distinguish the radio buttons because when we create GUI (Graphical User Interface) we make lot of radio button and there is a need to distinguish them as some radio button will be for choosing filter some will be for selecting color etc therefore need of naming is required.
In order to access we use accessibleName method and in order to set the name we use setAccessibleName method.
For setting name –
Syntax : radio_button.setAccessibleName(name)
Argument : It takes string as argument
Return : None
For accessing name –
Syntax : radio_button.accessibleName()
Argument : It takes no argument
Return : It return string
Implementation procedure :
1. Create a radio button
2. Set name to it with the help of setAccessibleName method
3. Create label to display the information
4. Access the name of radio button with the help of accessibleName method and store it in variable
5. Set this name to label with the help of setText method
Below is the implementation –
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" ) # setting name to radio button self .radio_button.setAccessibleName( "Geeky button" ) # creating label to display button name label = QLabel( self ) # setting geometry label.setGeometry( 200 , 200 , 150 , 30 ) # accessing the name name = self .radio_button.accessibleName() # showing name in label label.setText( "name = " + name) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # start the app sys.exit(App. exec ()) |
Output :