QCheckBox控件

一个QCheckBox会有2种状态:选中和为选中。它由一个选择框和一个label组成,常常用来表示应用的某些特性是启用或不启用。

在下面的例子中,我们创建了一个选择框,它的状态变化会引起窗口标题的变化。

示例的执行效果如下:

 1 #!/usr/bin/python3
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 ZetCode PyQt5 tutorial
 6 
 7 In this example, a QCheckBox widget
 8 is used to toggle the title of a window.
 9 
10 Author: Jan Bodnar
11 Website: zetcode.com
12 Last edited: August 2017
13 """
14 
15 from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
16 from PyQt5.QtCore import Qt
17 import sys
18 
19 
20 class Example(QWidget):
21 
22     def __init__(self):
23         super().__init__()
24 
25         self.initUI()
26 
27     def initUI(self):
28 
29         cb = QCheckBox('Show title', self)
30         cb.move(20, 20)
31         cb.toggle()     # 设置默认是选中状态
32 
33         # 将自定义的槽函数changeTitle和信号stateChanged绑定起来,
34         # 槽函数changeTitle会改变窗口的标题
35         cb.stateChanged.connect(self.changeTitle)
36 
37         self.setGeometry(300, 300, 250, 150)
38         self.setWindowTitle('QCheckBox')
39         self.show()
40 
41     def changeTitle(self, state):
42         # 选中状态下,窗口标题设置为”QCheckBox”,否则设置为空
43         if state == Qt.Checked:
44             self.setWindowTitle('QCheckBox')
45         else:
46             self.setWindowTitle(' ')
47 
48 
49 if __name__ == '__main__':
50 
51     app = QApplication(sys.argv)
52     ex = Example()
53     sys.exit(app.exec_())
原文地址:https://www.cnblogs.com/fuqia/p/8743189.html