PyQt5 – 复选框的checkState()方法

PyQt5 – 复选框的checkState()方法

在复选框中基本上有两种状态,即选中或未选中,尽管使用setTristate方法我们可以添加一个中间状态。

checkState方法用于检查复选框的状态,它返回CheckState对象,但当我们打印它时,输出将是如下的。

  • 0代表未检查的状态
  • 2代表检查状态
  • 1代表中间状态

语法: checkbox.checkState()

参数: 它不需要参数。

返回: 它返回CheckState对象

下面是实现方法。

# 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)
  
        # getting the checked state
        check = checkbox.checkState()
  
        # printing the state
        print("Before check status : ", check)
  
        # checking the check box
        checkbox.setChecked(True)
  
        # getting the checked state
        check = checkbox.checkState()
  
        # printing the state
        print("After check status : ", check)
  
        # exiting the program
        sys.exit()
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出:

Before check status :  0
After check status :  2

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程