1

I have this dictionary

myDict = {'key1' : 'val1', 'key2' : 'val2', 'key3' : 'val3'}

and I am iteratively using 'key' and 'value' of 'myDict' to create check and radio buttons.

for key, value in myDict.items():
    myDict[key] = Variable()
    myDict[value] = Variable()
    c = Checkbutton(root, text=key, variable=myDict[key], onvalue=key, offvalue="").pack(anchor=W)
    
    Radiobutton(root, text='radio1', variable=myDict[value], value=1).pack(anchor=E)
    Radiobutton(root, text='radio2', variable=myDict[value], value=2).pack(anchor=E)

    var_list.append(myDict[key]) #var_list is an empty list defined already
print(var_list) 

and I am getting this error

Traceback (most recent call last):
File "Check_box_lis_0305.py", line 21, in <module>
for key, value in myDict.items():
RuntimeError: dictionary changed size during iteration

Could you please help me to resolve this.

Ailurophile
  • 2,552
  • 7
  • 21
  • 46
  • Did this help you https://stackoverflow.com/questions/11941817/how-to-avoid-runtimeerror-dictionary-changed-size-during-iteration-error ? – Franco Morero May 04 '21 at 03:34
  • Neither of the comments here are helpful. You need to store the `Variable` values in a separate spot. Remember, that's where tkinter will store the result of the check or radio. – Tim Roberts May 04 '21 at 03:39

2 Answers2

1
checks = {}
radios = {}
for key, value in myDict.items():
    checks[key] = Variable()
    radios[key] = Variable()
    c = Checkbutton(root, text=key, variable=checks[key], onvalue=key, offvalue="").pack(anchor=W)
    
    Radiobutton(root, text='radio1', variable=radios[key], value=1).pack(anchor=E)
    Radiobutton(root, text='radio2', variable=radios[key], value=2).pack(anchor=E)

    var_list.append(checks[key]) #var_list is an empty list defined already
print(var_list) 

Now, when you get a click event, checks[key] will have the value of the check button, and radios[key] will have the selection for the radio button. I'm not sure why you need var_list here.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • I used `radios[value]` instead of `radios[key]`, does that make sense? – Vishwanath DH May 04 '21 at 04:49
  • my bigger task here is to create 3 lists. `def add_to_list(): listA = [] listD = [] listC = [] for i in var_list: if i.get() != "": listA.append(i.get()) if radios[value].get() == 1: listD.append(i.get()) else: listC.append(i.get()) print(listA) print(listD) print(listC) Button(root, text='OK', command=add_to_list).pack(anchor=W)` – Vishwanath DH May 04 '21 at 05:11
  • Sorry for the bad formatting, here the idea is to add all checked buttons to `listA` and for all the checked buttons add the key to `listD` if `radios[value]==1` and to `listC` otherwise. However the list being assigned to either `listD` or `listC` based on the radio selection of last key in the list(last loop key). Sorry if I confused you – Vishwanath DH May 04 '21 at 05:16
  • Don't type big chunks of code in comments. Add an update to your question. – Tim Roberts May 04 '21 at 06:04
0

The problem is that you are changing the dictionary during the for loop with

myDict[key] = Variable()
myDict[value] = Variable()
njp
  • 620
  • 1
  • 3
  • 16