1

I am trying to print the values of the checkboxes I select. Can someone help point me in the right direction? I am getting an error on this line - print(my_items[item])

This is the error:

TypeError: list indices must be integers or slices, not IntVar

import tkinter as tk
from tkinter import *

top = tk.Tk()
top.wm_title("Checklist")
my_items = ['pizza','breadsticks','wings','CocaCola','brownie'] 
check_boxes = {item:IntVar() for item in my_items}

def confirm():
    for item in check_boxes.values():
        if item.get() == 1:
            print(my_items[item])

for item in my_items:
    CB = Checkbutton(top, text = item, variable = check_boxes[item], anchor = W,  onvalue = 1, offvalue = 0, height=1, width = 50)
    CB.pack()

B1 = tk.Button(top, text = "confirm", command = confirm)
B1.pack()


top.mainloop()
4
  • Please edit your question to include the error. Commented Jan 20, 2020 at 20:00
  • The problem is that item is not a valid index to access a list. Sorry, bur I don't have a clue about TKinter, but in the meantime, you can take a look to this: stackoverflow.com/questions/50048561/… Commented Jan 20, 2020 at 20:39
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. Commented Jan 20, 2020 at 20:41
  • see what you have in print( item ). List my_items needs number but item is not number. You have to rewrite all in different way. Why don't get keys from dictionary? Commented Jan 20, 2020 at 20:44

1 Answer 1

3

If you created dictionary like

check_boxes  = {'pizza': IntVar(), ...}

then you should use keys and values from dictionary

In key you get text 'pizza' which you need

def confirm():
    for key, item in check_boxes.items():
        if item.get() == 1:
            print(key)

import tkinter as tk
# from tkinter import * # PEP8: `import *` is not preferred

# --- functions ---

def confirm():
    for key, item in check_boxes.items():
        if item.get() == 1:
            print(key)

# --- main ---

my_items = ['pizza', 'breadsticks', 'wings', 'CocaCola', 'brownie'] 

root = tk.Tk()

check_boxes = {item:tk.IntVar() for item in my_items}

for item in my_items:
    cb = tk.Checkbutton(root, text=item, variable=check_boxes[item], anchor='w', onvalue=1, offvalue=0, width=50)
    cb.pack()

b1 = tk.Button(root, text="confirm", command=confirm) # PEP8: without spaces around `=`, `lower_case_names` for variables
b1.pack()

root.mainloop()

BTW: You can also use StringVar() and onvalue='pizza'

import tkinter as tk

# --- functions ---

def confirm():
    for string_var in check_boxes:
        text = string_var.get()
        if text:
            print(text)

# --- main ---

my_items = ['pizza', 'breadsticks', 'wings', 'CocaCola', 'brownie'] 

root = tk.Tk()

check_boxes = []

for item in my_items:
    string_var = tk.StringVar()
    check_boxes.append(string_var)

    cb = tk.Checkbutton(root, text=item, variable=string_var, anchor='w', onvalue=item, offvalue='', width=50)
    cb.pack()

b1 = tk.Button(root, text="confirm", command=confirm)
b1.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful. Thank you!

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.