PyQt5 QCheckBox复选框

QCheckBox复选框


PyQt5 QCheckBox复选框_第1张图片

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt

class MyCheckBoxWindow(QWidget):
    def __init__(self, parent=None):
        super(MyCheckBoxWindow, self).__init__(parent)
        
        groupBox = QGroupBox("Checkboxes")
        groupBox.setFlat(True)

        layout = QHBoxLayout()
        self.checkBox1 = QCheckBox("&Checkbox1")
        self.checkBox1.setChecked(True)
        self.checkBox1.stateChanged.connect(lambda: self.btnstate(self.checkBox1))
        layout.addWidget(self.checkBox1)

        self.checkBox2 = QCheckBox("CheckBox2")
        self.checkBox2.toggled.connect(lambda:self.btnstate(self.checkBox2))
        layout.addWidget(self.checkBox2)

        self.checkBox3 = QCheckBox("CheckBox3")
        self.checkBox3.setTristate(True)
        self.checkBox3.setCheckState(Qt.PartiallyChecked)
        self.checkBox3.stateChanged.connect(lambda: self.btnstate(self.checkBox3))
        layout.addWidget(self.checkBox3)

        groupBox.setLayout(layout)
        mainlayout = QVBoxLayout()
        mainlayout.addWidget(groupBox)

        self.setLayout(mainlayout)
        self.setWindowTitle("CheckBox Demo")

    def btnstate(self, btn):
        chk1Status = self.checkBox1.text() +", isChecked="+str(self.checkBox1.isChecked())+'checkState='+str(self.checkBox1.checkState())+"\n"
        chk2Status = self.checkBox2.text() +", isChecked="+str(self.checkBox1.isChecked())+'checkState='+str(self.checkBox2.checkState())+"\n"
        chk3Status = self.checkBox3.text() +", isChecked="+str(self.checkBox3.isChecked())+'checkState='+str(self.checkBox3.checkState())+"\n"
        print(chk1Status + chk2Status + chk3Status)
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyCheckBoxWindow()
    win.show()
    sys.exit(app.exec_())

PyQt5 QCheckBox复选框_第2张图片

你可能感兴趣的:(PyQt5,qt,python,开发语言)