Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Last active July 16, 2017 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/c1b0ffbe5c01ea576ab7e5b46a7dadb7 to your computer and use it in GitHub Desktop.
Save eyllanesc/c1b0ffbe5c01ea576ab7e5b46a7dadb7 to your computer and use it in GitHub Desktop.
CheckBoxExample
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QCheckBox, QLineEdit
from functools import partial
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QVBoxLayout())
self.le = QLineEdit(self)
self.layout().addWidget(self.le)
self.data_dictionary = {'key1': '0', 'key2': '0', 'key3': '0', 'key4': '0'}
self.le.setText(str(self.data_dictionary))
for key in self.data_dictionary.keys():
temp_check = QCheckBox(self)
temp_check.setText(key)
temp_check.stateChanged.connect(partial(self.updateDataDictionary, key, temp_check))
temp_check.stateChanged.connect(lambda: self.le.setText(str(self.data_dictionary)))
self.layout().addWidget(temp_check)
def updateDataDictionary(self, key, check):
if check.isChecked():
self.data_dictionary[key] = "1"
else:
self.data_dictionary[key] = "0"
def main():
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment