0

I am reading a csv file and according to the number of rows in the csv, I have created that many number of check boxes. There is a submit button on the screen and after clicking that I want to check which all check boxes have been ticked but right now it is checking whether the box is ticked at the time of check box creation.

I have created the check boxes using a for loop and because of this, I don't have the name of each individual check box and hence the only way I can check if the check box is ticked is by running the .isChecked() function in the for loop. Hence it is checking the check boxes when the for loop runs.

opened csv and counted the no. of rows#### appended the value of the row we want into list 'a'
   with open("fike_path") as f:
        reader = csv.reader(f, delimiter=",")
        next(reader)
        for i in reader:
            a.append(i[4])
    print(a)

    j=0               
created check boxes acc. to no. of rows
   for i in range(len(a)):

      cb = QCheckBox(a[i], self)           
      cb.toggle()
      cb.resize(200,28)
      cb.setChecked(False)
      cb.move(720,135+j)

      j=j+15

      if cb.isChecked() == False:   ####checking if check box is ticked####
              print (cb.text()+" is not selected")
      else:
              print("Is selected")

Expected result: print("is selected") for the check boxes which have been ticked and print("is not selected") along with check box value for the boxes which have not been ticked.

Actual result: print("is not selected") for all check boxes as it checks when the for loop is run i.e. the time of creation where default value is false.

AnaDesh
  • 23
  • 11
  • can't you keep checkboxes on list and check them later ? You could also assign function to CheckBox which will be executed when it changes state. – furas Jul 04 '19 at 09:00
  • instead of `for i in range(len(a)) : cb = QCheckBox(a[i], self)` you could do `for item in a : cb = QCheckBox(item, self)`. You could also use names for variables which means something - it makes code more readable. – furas Jul 04 '19 at 09:03
  • provide a [MRE] – eyllanesc Jul 04 '19 at 09:45
  • I created the list of checkboxes (cblist) in a class and now I want to pass it through the function- "self.btn.clicked.connect(self.onButtonClicked(cblist))" to onButtonClicked(self,listofcb) but I am getting an error- TypeError: argument 1 has unexpected type 'NoneType' – AnaDesh Jul 04 '19 at 10:04
  • @AnaDesh use `self.btn.clicked.connect(lambda *args, cblist=cblist: self.onButtonClicked(cblist))` – eyllanesc Jul 04 '19 at 10:21
  • @eyllanesc thank you! it worked by passing the arguments you provided – AnaDesh Jul 04 '19 at 11:23

0 Answers0