0

I'm trying to figure out how to get the text values from all of the Checkbuttons that have been selected when the user clicks the Submit button.So far I am able to get the index numbers for the buttons that have been selected but cant get the text values.For an example, if the first,second and fifth button were checked it would print 1,2,5 but not the actual text values. Iv referenced other post about using .cget() but had no luck. My next thought is using a dictionary to store the number and text values together but the only issue I plan on making the list much larger. Iv posted the code and a picture below to help explain.Any suggestions?

from tkinter import *

sick = []

def getChecked():
   for i in range(len(sick)):
    selected = ""
    if sick[i].get() >= 1:
       selected += str(i)
       print(selected)


 root = Tk()
 root.geometry('850x750')
 root.title("Registration Form")

for i in range(6):
    option = IntVar()
    option.set(0)
sick.append(option)


   # Conditions checkbutton
 label_6 = Label(root, text="Have you ever had ( Please check all that apply ) :", width=50, font= 
                 ("bold", 10))
 label_6.place(x=35, y=330)

Checkbutton(root, command=getChecked, text="Anemia", variable=sick[0]).place(x=130, y=350)

Checkbutton(root, command=getChecked, text="Asthma", variable=sick[1]).place(x=270, y=350)

Checkbutton(root, command=getChecked, text="Arthritis", variable=sick[2]).place(x=410, y=350)

Checkbutton(root, command=getChecked, text="Cancer", variable=sick[3]).place(x=560, y=350)

Checkbutton(root, command=getChecked, text="Gout", variable=sick[4]).place(x=130, y=380)

Checkbutton(root, command=getChecked, text="Diabetes", variable=sick[5]).place(x=270, y=380)

# submit button
Button(root, text='Submit', command=getChecked, width=20, bg='brown', fg='white').place(x=180, y=600)

root.mainloop()
3
  • variable argument should be a variable like StringVar() or IntVar() rather than indexes from a list. So try var=StringVar() and then pass that as the variable argument variable=var and later just say var.get() to get the value, if I'm not wrong Commented Aug 25, 2020 at 19:54
  • You also have arguments like offvalue and onvalue for the checkbuttons, do take a look at them Commented Aug 25, 2020 at 19:57
  • 1
    Okay I get what you are saying, let me change my code a bit and Ill let you know if that worked. Commented Aug 25, 2020 at 20:02

1 Answer 1

1

You need to move sick.append(option) inside your loop

for i in range(6):
    option = IntVar()
    option.set(0)
    sick.append(option)

Also, it can be more efficient if you use the values you want as the values for the checkbutton.

For example, start by using StringVar, and initialize the value to the empty string:

for i in range(6):
    option = StringVar(value="")
    sick.append(option)

Next, set the onvalue to the value you want for the checkbutton, and the offvalue to be the empty string:

Checkbutton(..., variable=sick[0], onvalue="Anemia", offvalue="")
Checkbutton(..., variable=sick[1], onvalue="Athma", offvalue="")
Checkbutton(..., variable=sick[2], onvalue="Arthritis", offvalue="")
Checkbutton(..., variable=sick[3], onvalue="Cancer", offvalue="")
Checkbutton(..., variable=sick[4], onvalue="Gout", offvalue="")
Checkbutton(..., variable=sick[5], onvalue="Diabetes", offvalue="")

Now, printing the values can be done like this:

def getChecked():
    for var in sick:
        value = var.get()
        if value:
            print(value)

Or perhaps something even shorter to print a comma-separated list:

def getChecked():
    values = [var.get() for var in sick if var.get()]
    print("choices:", ", ".join(values))

The advantage to this is that the values don't necessarily have to be the same as the labels. For example, if these values are going to a database and need to be all lowercase, the labels can be capitalized but the values can be lowercase.

Sign up to request clarification or add additional context in comments.

Comments

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.