📜  PyQt5 – 复选框的 checkState() 方法

📅  最后修改于: 2022-05-13 01:55:49.862000             🧑  作者: Mango

PyQt5 – 复选框的 checkState() 方法

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

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

  • 0 表示未选中状态
  • 2 用于检查状态
  • 1 代表中间状态

下面是实现。

# 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