1

I have two checkbox Pass and FAIL I am parsing the csv for column1 and adding the two checkbox .

        X = 100
        Y = 71
        for item in column1[key]:
            if item != '':
                listbox.insert('end', item)
                chk_state1 = tk.IntVar()
                tk.Checkbutton(self.root, text="PASS",variable=chk_state1,font=("Arial Bold", 8),).place(x=X,y=Y)
                chk_state2 = tk.IntVar()
                tk.Checkbutton(self.root, text="FAIL",variable=chk_state2,font=("Arial Bold", 8),).place(x=X+80,y=Y)
                Y = Y +20
  1. How to know which row of column1 Checkbox is selected
  2. At a time only one checkbox should be selected

Any inputs will be helpful thanks in advance

Piu
  • 19
  • 5
  • For item 2, why don't you just use one checkbox for each item: checked for PASS, otherwise for FAIL. Or use radiobuttons instead of checkboxes. For item 1, suggest to use a dictionary to store those tkinter variables (`IntVar()`) and use `item` as the key. – acw1668 Sep 15 '21 at 15:06
  • Does not understood can you elaborate more – Piu Sep 15 '21 at 17:26

1 Answers1

0

For item 1, use a dictionary (item names as the keys) to hold the created tkinter IntVars. Then you can get the checked state for each item by going through the dictionary later.

For item 2, you can use Radiobutton instead of Checkbutton.

        X = 100
        Y = 71

        myfont = ('Arial Bold', 8)
        self.cblist = {}
        for item in column1[key]:
            if item != '':
                listbox.insert('end', item)
                chk_state = tk.IntVar(value=0)
                tk.Radiobutton(self.root, text='PASS', variable=chk_state, value=1, font=myfont).place(x=X, y=Y)
                tk.Radiobutton(self.root, text='FAIL', variable=chk_state, value=0, font=myfont).place(x=X+80, y=Y)
                self.cblist[item] = chk_state
                Y += 22

        tk.Button(self.root, text='Check', command=self.check).place(x=X, y=Y+10)

    ...

    def check(self):
        result = ('Fail', 'Pass')
        print(', '.join(f'{item}:{result[var.get()]}' for item, var in self.cblist.items()))

Note that I have added a button to print out the selected state for each item.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • I tried this code , in print i am getting all items instead of particular items for that pass or fail row – Piu Sep 16 '21 at 05:22
  • @Piu The `check()` is just an example of getting the checked state for each item, you can modify it to filter out items, for example only show PASS items. – acw1668 Sep 16 '21 at 05:26