PyQt5 – 设置和访问单选按钮的名称

PyQt5 – 设置和访问单选按钮的名称

在这篇文章中,我们将看到如何设置和访问单选按钮的名称。名称基本上是用来区分单选按钮的,因为当我们创建GUI(图形用户界面)时,我们会做很多单选按钮,有必要区分它们,因为有些单选按钮是用来选择过滤器的,有些是用来选择颜色的,等等,所以需要命名。

为了访问,我们使用accessibleName方法,为了设置名称,我们使用setAccessibleName方法。

对于设置名称 –

语法: radio_button.setAccessibleName(name)

参数: 它需要字符串作为参数

返回:

对于访问名称 –

语法: radio_button.accessibleName()

参数: 它没有参数

返回: 它返回字符串

实现过程:

1.创建一个单选按钮

2.在setAccessibleName方法的帮助下为其设置名称

3.创建标签来显示信息

4.在accessibleName方法的帮助下访问单选按钮的名称,并将其存储在变量

5.在setText方法的帮助下,将这个名字设置到标签上

以下是实现方法 –

# 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())

输出 :

PyQt5 - 设置和访问单选按钮的名称

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程