0

I want to check (let say 3) checkboxes from 5. But by my code, it is only possible to select 1. The following loop in defined in a function and displays all choices as checkboxes. I am wondering how can I check 2 or 3 boxes instead of just 1 (after run the code).

    for g in range(NOP):
    cb11 = Checkbutton(root, text=str(Players[g]), variable=sp11, onvalue=str(Players[g]), offvalue='', font=('helvetica', 11,'bold'), fg='silver', bg='#263D42')
    cb11.deselect()
    canvas.create_window((W/10)+dis, 75, window=cb11)
    dis += sys.getsizeof(str(Players[g]))

Thank you for your help guys, Bests

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • ***"how can I check ... (after run the code)."***: Save a reference of the `Checkbutton` to `Players[g]` or follow this approach: [How to access one Button in a grid of Button.](https://stackoverflow.com/a/59073175/7414759) – stovfl Mar 09 '20 at 11:21

1 Answers1

0

I found the answer. sp11 is just 1 variable that defines the checkmark for each checkbox. Accordingly, I need to assign a unique variable for each checkbox. To do so, I should change the code as below:

    sp11=[]
    for g in range(NOP):
    SV = StringVar()
    sp11.append(SV)
    cb11 = Checkbutton(root, text=str(Players[g]), variable=sp11[g], onvalue=str(Players[g]), offvalue='', font=('helvetica', 11,'bold'), fg='silver', bg='#263D42')
    cb11.deselect()
    canvas.create_window((W/10)+dis, 75, window=cb11)
    dis += sys.getsizeof(str(Players[g]))