PyQt5 – Check Box的setChecked()方法

PyQt5 – Check Box的setChecked()方法

setChecked方法是用来改变复选框的状态。在默认情况下,点击复选框部件后,它的状态会被改变为选中,但在setChecked方法的帮助下,我们可以不点击它而直接完成。

语法: checkbox.setChecked(True)

参数: 它以bool作为参数。

执行的动作: 它将改变复选框的状态。

下面是实现方法。

# 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('Check Box', self)
  
        # setting geometry of check box
        checkbox.setGeometry(200, 150, 100, 30)
  
        # setting check box state to checked
        checkbox.setChecked(True)
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :

PyQt5 - Check Box的setChecked()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程