PyQt5 – 如何阻止单选按钮被选中
在本文中,我们将看到如何停止单选按钮以进行检查,有时需要阻止单选按钮以使用户无法检查它。
为了阻止单选按钮被选中,我们必须更改单选按钮的可检查属性并将其设置为 False。
Syntax : radio_button.setCheckable(False)
Argument : It takes bool as argument
Action : It will not allow button to get checked.
下面是实现。
# 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")
# changing check-able property of radio button
self.radio_button.setCheckable(False)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :