PyQt5 - isChecked() method for Check Box

PyQt5 - isChecked() method for Check Box

In PyQt5, the isChecked() method is used to determine if a QCheckBox (or other checkable items) is currently checked. It returns True if the checkbox is checked and False otherwise.

Here's a simple example demonstrating the use of isChecked() method with a QCheckBox:

Example:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QCheckBox, QPushButton, QLabel class App(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.checkbox = QCheckBox("Check Me!", self) layout.addWidget(self.checkbox) btn = QPushButton("Check Status", self) btn.clicked.connect(self.showStatus) layout.addWidget(btn) self.label = QLabel("Checkbox Status: Not Checked", self) layout.addWidget(self.label) self.setLayout(layout) self.setWindowTitle('QCheckBox isChecked Demo') self.show() def showStatus(self): if self.checkbox.isChecked(): self.label.setText("Checkbox Status: Checked") else: self.label.setText("Checkbox Status: Not Checked") if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

In this example:

  • A QCheckBox is created with the label "Check Me!".
  • A QPushButton labeled "Check Status" is used to trigger a method (showStatus) that checks the status of the checkbox.
  • The showStatus method uses the isChecked() method to determine if the checkbox is currently checked and then updates a QLabel with the status.

When you run this example, you can check/uncheck the checkbox and click the button to see the current status displayed in the label.


More Tags

applet save-as double-submit-problem chm nsurlprotocol zsh-completion phpredis bind binary-tree code-conversion

More Programming Guides

Other Guides

More Programming Examples