PyQt5 – 复选框的 nextCheckState() 方法
我们可以使用setChecked
设置复选框的状态,或者对于三态复选框,我们可以使用setCheckState
方法设置其状态。
nextCheckState
设置复选框的下一个状态,即默认复选框,如果选中初始检查状态,它将设置为未选中,反之亦然。对于三态复选框,如果选中状态,则将其设置为中间状态,如果初始状态为中间状态,则将其设置为已选中,如果已选中,则将其设置为未选中。
Syntax : checkbox.nextCheckState()
Argument : It takes no argument.
Action performed : It will change the state of check box according to the previous state.
下面是实现。
# 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 it tristate check box
checkbox.setTristate(True)
# setting check box to its next state
checkbox.nextCheckState()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :