#QCheckBox
#三种状态
#未选中: 0
#半选中: 1
#选中: 2
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class QCheckboxDemo(QWidget):
def __init__(self):
super(QCheckboxDemo,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('复选框控件')
layout = QHBoxLayout()
self.checkBox1 = QCheckBox('复选框1')
self.checkBox1.setChecked(True)
self.checkBox1.stateChanged.connect(self.checkboxstate)
self.checkBox2 = QCheckBox('复选框2')
#self.checkBox2.setChecked(True)
self.checkBox2.stateChanged.connect(self.checkboxstate)
self.checkBox3 = QCheckBox('半选中')
self.checkBox3.setTristate(True)
self.checkBox3.setCheckState(Qt.PartiallyChecked)
self.checkBox3.stateChanged.connect(self.checkboxstate)
layout.addWidget(self.checkBox1)
layout.addWidget(self.checkBox2)
layout.addWidget(self.checkBox3)
self.setLayout(layout)
def checkboxstate(self):
sender = self.sender()
print(sender.text()+'ischecked='+str(sender.isChecked())+ 'state:'+ str(sender.checkState()))
if __name__ == '__main__':
app =QApplication(sys.argv)
main = QCheckboxDemo()
main.show()
app.exit(app.exec_())
复选框控件
最新推荐文章于 2024-07-25 23:36:34 发布