Python3 PyQt4 Creating a simple QCheckBox and changing a Boolean variable

前端 未结 2 1776
生来不讨喜
生来不讨喜 2021-01-15 00:54

So I have been trying to write a GUI using Python 3.3 and PyQt4. I have been through a few tutorials and I still can\'t figure out how to have a Checkbox checking and unchec

2条回答
  •  情书的邮戳
    2021-01-15 01:51

    The checkbox emits a stateChanged event when its state is changed (really!). Connect it to an event handler:

    import sys
    
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    class SelectionWindow(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.ILCheck = False
    
            ILCheckbox = QCheckBox(self)
            ILCheckbox.setCheckState(Qt.Unchecked)
    
            ILCheckbox.stateChanged.connect(self.ILCheckbox_changed)
    
            MainLayout = QGridLayout()
            MainLayout.addWidget(ILCheckbox, 0, 0, 1, 1)
    
            self.setLayout(MainLayout)
    
        def ILCheckbox_changed(self, state):
            self.ILCheck = (state == Qt.Checked)
    
            print(self.ILCheck)
    
    
    if __name__ == '__main__':
      app = QApplication(sys.argv)
      window = SelectionWindow()
    
      window.show()
      sys.exit(app.exec_())
    

提交回复
热议问题