qt - Change checkbox state to not checked when other checkbox is checked pyqt

Qt - Change checkbox state to not checked when other checkbox is checked pyqt

In PyQt, you can connect the stateChanged signal of one checkbox to a slot that updates the state of another checkbox. Here's an example:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout class MainWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Checkbox Example') layout = QVBoxLayout() # Checkbox 1 self.checkbox1 = QCheckBox('Checkbox 1') layout.addWidget(self.checkbox1) # Checkbox 2 self.checkbox2 = QCheckBox('Checkbox 2') layout.addWidget(self.checkbox2) # Connect checkbox1 stateChanged signal to checkbox1_state_changed slot self.checkbox1.stateChanged.connect(self.checkbox1_state_changed) self.setLayout(layout) def checkbox1_state_changed(self, state): if state == 2: # Checked state self.checkbox2.setChecked(False) # Set checkbox2 to not checked state if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 

In this example:

  • We create two checkboxes (checkbox1 and checkbox2) and add them to a vertical layout.
  • We connect the stateChanged signal of checkbox1 to the checkbox1_state_changed slot.
  • In the checkbox1_state_changed slot, if checkbox1 is checked (state == 2), we set checkbox2 to the unchecked state (setChecked(False)).

This way, whenever checkbox1 is checked, checkbox2 will be automatically unchecked.

Examples

  1. "How to uncheck a checkbox when another checkbox is checked in PyQt?" Description: This query explores methods to ensure that when one checkbox is checked, another checkbox is automatically unchecked in a PyQt application.

    from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout import sys class CheckBoxExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.checkbox1 = QCheckBox('Checkbox 1') self.checkbox1.stateChanged.connect(self.on_checkbox1_changed) layout.addWidget(self.checkbox1) self.checkbox2 = QCheckBox('Checkbox 2') self.checkbox2.stateChanged.connect(self.on_checkbox2_changed) layout.addWidget(self.checkbox2) self.setLayout(layout) self.setWindowTitle('Checkbox Example') self.show() def on_checkbox1_changed(self, state): if state == 2: # Checked state self.checkbox2.setChecked(False) def on_checkbox2_changed(self, state): if state == 2: # Checked state self.checkbox1.setChecked(False) if __name__ == '__main__': app = QApplication(sys.argv) ex = CheckBoxExample() sys.exit(app.exec_()) 
  2. "PyQt: How to synchronize checkbox states in PyQt application?" Description: This query focuses on synchronizing checkbox states in a PyQt application, ensuring that when one checkbox is checked, others are unchecked.

    from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout import sys class CheckBoxExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.checkbox1 = QCheckBox('Checkbox 1') self.checkbox1.stateChanged.connect(self.sync_checkboxes) layout.addWidget(self.checkbox1) self.checkbox2 = QCheckBox('Checkbox 2') self.checkbox2.stateChanged.connect(self.sync_checkboxes) layout.addWidget(self.checkbox2) self.setLayout(layout) self.setWindowTitle('Checkbox Example') self.show() def sync_checkboxes(self, state): sender = self.sender() if sender == self.checkbox1: if state == 2: # Checked state self.checkbox2.setChecked(False) elif sender == self.checkbox2: if state == 2: # Checked state self.checkbox1.setChecked(False) if __name__ == '__main__': app = QApplication(sys.argv) ex = CheckBoxExample() sys.exit(app.exec_()) 
  3. "How to handle checkbox states in PyQt: Uncheck other checkboxes when one is checked?" Description: This query seeks ways to manage checkbox states in a PyQt application, ensuring that only one checkbox can be checked at a time.

    from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout import sys class CheckBoxExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.checkbox1 = QCheckBox('Checkbox 1') self.checkbox1.clicked.connect(self.on_checkbox1_clicked) layout.addWidget(self.checkbox1) self.checkbox2 = QCheckBox('Checkbox 2') self.checkbox2.clicked.connect(self.on_checkbox2_clicked) layout.addWidget(self.checkbox2) self.setLayout(layout) self.setWindowTitle('Checkbox Example') self.show() def on_checkbox1_clicked(self): if self.checkbox1.isChecked(): self.checkbox2.setChecked(False) def on_checkbox2_clicked(self): if self.checkbox2.isChecked(): self.checkbox1.setChecked(False) if __name__ == '__main__': app = QApplication(sys.argv) ex = CheckBoxExample() sys.exit(app.exec_()) 
  4. "PyQt checkbox: How to manage checkbox states to avoid multiple checks?" Description: This query involves managing PyQt checkbox states to prevent multiple checkboxes from being checked simultaneously.

    from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout import sys class CheckBoxExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.checkbox1 = QCheckBox('Checkbox 1') self.checkbox1.stateChanged.connect(self.on_checkbox_state_changed) layout.addWidget(self.checkbox1) self.checkbox2 = QCheckBox('Checkbox 2') self.checkbox2.stateChanged.connect(self.on_checkbox_state_changed) layout.addWidget(self.checkbox2) self.setLayout(layout) self.setWindowTitle('Checkbox Example') self.show() def on_checkbox_state_changed(self, state): sender = self.sender() if state == 2: # Checked state checkboxes = [self.checkbox1, self.checkbox2] for checkbox in checkboxes: if checkbox != sender: checkbox.setChecked(False) if __name__ == '__main__': app = QApplication(sys.argv) ex = CheckBoxExample() sys.exit(app.exec_()) 
  5. "PyQt: Implementing exclusive checkbox selection in a PyQt application" Description: This query discusses implementing exclusive checkbox selection in a PyQt application to ensure only one checkbox can be checked at a time.

    from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout import sys class CheckBoxExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.checkbox1 = QCheckBox('Checkbox 1') self.checkbox1.stateChanged.connect(self.on_checkbox_state_changed) layout.addWidget(self.checkbox1) self.checkbox2 = QCheckBox('Checkbox 2') self.checkbox2.stateChanged.connect(self.on_checkbox_state_changed) layout.addWidget(self.checkbox2) self.setLayout(layout) self.setWindowTitle('Checkbox Example') self.show() def on_checkbox_state_changed(self, state): sender = self.sender() if state == 2: # Checked state checkboxes = [self.checkbox1, self.checkbox2] for checkbox in checkboxes: if checkbox != sender: checkbox.setChecked(False) if __name__ == '__main__': app = QApplication(sys.argv) ex = CheckBoxExample() sys.exit(app.exec_()) 

More Tags

gatt battery kotlin-null-safety mysql-error-1241 summernote imagebackground lifecycle-hook signalr.client inline react-dom

More Programming Questions

More Genetics Calculators

More Physical chemistry Calculators

More Other animals Calculators

More Livestock Calculators