PyQt5 – 复选框的 setCheckState() 方法
当我们创建一个复选框时,它只有选中和未选中两种状态,尽管我们可以使用setTristate
方法添加一个中间状态。我们可以使用setChecked
将复选框设置为选中或取消选中,但使用此方法我们无法将复选框设置为中间状态。
setCheckState
方法用于设置复选框的状态,它可以选中或取消选中,甚至可以设置中间状态,它以 CheckState 对象为参数。
Syntax : checkbox.setCheckState(1)
Argument : It takes CheckState object as argument.
Action performed:
If we pass 0 to it, it set un-checked state to check box
If we pass 1 to it, it set intermediate state to check box
If we pass any other integer to it, it set checked state to check box
下面是实现。
# 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 the check-box
checkbox = QCheckBox('Geek ?', self)
# setting geometry of check box
checkbox.setGeometry(200, 150, 100, 40)
# making check box as tristate
checkbox.setTristate(True)
# setting check box state
checkbox.setCheckState(1)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :