0

I am trying to create a program that allows user to select from a list of symptoms they have and find the disease.i added a drop-down menu that contains the symptoms in checkboxes

clicked2=StringVar(root) 
clicked2.set(symptoms[0]) 
  
drop2=OptionsMenu(root,variable=clicked2,value="options :")
drop2.pack()
checked=[] #contain the checked symptoms

def checkedSymptom:
    if (var2.get()=1):
        checked.append(symptom)
        print(symptom)
    elif (var2.get()=0):
        pass

this is the part I'm having an issue with

for symptom in symptoms:
    var2=IntVar()
    drop2['menu'].add_checkbutton(label=symptom,onvalue=1,offvalue=0, variable=var2, command=checkedSymptom)

symptoms here is the list of symptoms

but since it loops through the list of symptoms each time it loops the variable that holds the checkbox is overwritten. so at the end only the last item on the list does anything when checked.

-

1
  • Use a dictionary to store those IntVar. Commented Aug 10, 2020 at 0:42

1 Answer 1

1

You can use a dictionary to store the variables:

vars = {}
for symptom in symptoms:
    var2 = IntVar(root)
    drop2['menu'].add_checkbutton(label=symptom, onvalue=1, offvalue=0, variable=var2, command=checkedSymptom)
    vars[symptom] = var2

Then use the dictionary inside checkedSymptom() to check which symptom is checked:

def checkedSymptom():
    checked.clear()
    checked.extend([key for key in vars if vars[key].get() == 1])
    print(checked)
Sign up to request clarification or add additional context in comments.

1 Comment

It took me a while to understand it but thanks it solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.