PyQt5 – 复选框的setCheckState()方法

PyQt5 – 复选框的setCheckState()方法

当我们创建一个复选框时,它只有两种状态,即选中和未选中,尽管我们可以使用setTristate方法添加一个中间状态。我们可以使用setChecked方法将复选框设置为选中或未选中,但使用这个方法我们无法将复选框设置为中间状态。

setCheckState方法用于设置复选框的状态,它可以被选中或不被选中,甚至可以设置中间状态,它需要CheckState对象作为参数。

语法: checkbox.setCheckState(1)

参数: 它使用CheckState对象作为参数。

执行的动作:

如果我们传给它0,它就为复选框设置未检查状态

如果我们传给它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)
  
        # 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())
Python

输出 :

PyQt5 - 复选框的setCheckState()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册