PyQt5 - 单选按钮的描述
在本文中,我们将了解如何设置和访问单选按钮的描述。描述基本上是关于单选按钮的详细信息,因为当我们创建 GUI(图形用户界面)时,我们制作了很多单选按钮,并且需要描述它们,因为一些单选按钮将用于选择过滤器,一些单选按钮将用于选择颜色等需要描述是必需的。描述将告诉使用和过程我们需要了解的有关特定单选按钮的所有信息。
为了访问我们使用accessibleDescription
方法,为了设置描述我们使用setAccessibleDescription
方法。
For setting description –
Syntax : radio_button.setAccessibleDescription(info)
Argument : It takes string as argument
Return : None
For accessing description –
Syntax : radio_button.accessibleDescription()
Argument : It takes no argument
Return : It return string
实施程序:
1.创建一个单选按钮
2. 借助 setAccessibleDescription 方法为其设置描述
3.创建标签以显示信息
4.通过accessibleDescription方法访问单选按钮的描述并将其存储在变量中
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 description to radio button
self.radio_button.setAccessibleDescription(
"This is a Geeky button to know if you are geek or not")
# creating label to display button name
label = QLabel(self)
# setting geometry
label.setGeometry(100, 200, 400, 30)
# accessing the description
info = self.radio_button.accessibleDescription()
# showing description in label
label.setText("Description " + info)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :