PyQt5 – 复选框的 nextCheckState() 方法
我们可以使用setChecked来设置复选框的状态,或者对于三态复选框,我们可以使用setCheckState方法来设置它的状态。 nextCheckState设置复选框的下一个状态,即对于默认复选框,如果初始复选状态是选中,它将设置为未选中,反之亦然。对于三态复选框,如果复选状态是未选中,它将设置为中间状态,如果初始状态是中间状态,它将设置为选中,如果是选中,它将设置为未选中。
语法: checkbox.nextCheckState()
参数: 它不需要参数。
执行的动作: 它将根据先前的状态改变复选框的状态。
下面是实现方法。
# 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())
输出 :