from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.checkbox = QCheckBox('Checkbox', self) self.checkbox.stateChanged.connect(self.checkbox_changed) vbox = QVBoxLayout() vbox.addWidget(self.checkbox) self.setLayout(vbox) self.setWindowTitle('QCheckBox Example') self.show() def checkbox_changed(self, state): if state == 0: print('Checkbox unchecked') else: print('Checkbox checked') app = QApplication([]) ex = Example() app.exec_()
from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout, QPushButton class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.checkbox1 = QCheckBox('Option 1', self) self.checkbox2 = QCheckBox('Option 2', self) self.checkbox3 = QCheckBox('Option 3', self) self.submit_button = QPushButton('Submit', self) self.submit_button.clicked.connect(self.submit) vbox = QVBoxLayout() vbox.addWidget(self.checkbox1) vbox.addWidget(self.checkbox2) vbox.addWidget(self.checkbox3) vbox.addWidget(self.submit_button) self.setLayout(vbox) self.setWindowTitle('QCheckBox Example') self.show() def submit(self): selected = [] if self.checkbox1.isChecked(): selected.append('Option 1') if self.checkbox2.isChecked(): selected.append('Option 2') if self.checkbox3.isChecked(): selected.append('Option 3') print(f'Selected: {", ".join(selected)}') app = QApplication([]) ex = Example() app.exec_()These examples use the PyQt5.QtWidgets package library.