Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # https://www.reddit.com/r/learnpython/comments/4wkzva/pyqt_child_of_qcheckbox_with_additional_argument/
- #
- from __future__ import print_function
- import sys
- from PyQt4 import QtGui, QtCore
- class Window(QtGui.QDialog):
- def __init__(self, window):
- super(Window, self).__init__() # space after `,`
- self.mw = window
- self.setGeometry(50, 50, 500, 300) # space after `,`
- self.setWindowTitle("PQ status!")
- self.letters_checked = [] # lower_case name
- self.all_letters = ['a','b','c','d','e'] # space after `,` # lower_case name
- self.home()
- self.show()
- def home(self):
- # pythonic method to work with list
- for index, letter in enumerate(self.all_letters):
- self.checkBox = MyCheckBox(letter, self)
- self.checkBox.stateChanged.connect(self.checkBox.add_or_remove_letter)
- self.checkBox.move(20, 20*index)
- class MyCheckBox(QtGui.QCheckBox):
- def __init__(self, text, window):
- self.text = text
- self.window = window # add to class to use `self.window` instead of global `window`
- super(MyCheckBox, self).__init__(text, window) # need parent `window/widget`
- def add_or_remove_letter(self, state):
- if state == QtCore.Qt.Checked:
- self.window.letters_checked.append(self.text) # with `self.`
- elif self.text in self.window.letters_checked: # `test` without () # there is no `deck`
- self.window.letters_checked.remove(self.text) # with `self.`
- print('[DEBUG] letters_checked:', ', '.join(self.window.letters_checked))
- if __name__ == '__main__':
- app = QtGui.QApplication(sys.argv)
- GUI = Window(QtGui.QMainWindow)
- app.exec_()
Add Comment
Please, Sign In to add comment