I am making a menu test for the employees at my restaurant. The plan is for menu items to loop through “Loop items here” at which time they select the correct checkbuttons (ingredients) and then click the “submit and continue button“. When they click the submit button I first need to read the on and off values of the checkbuttons to determine which items they have selected, then compare those to the correct answers that I defined in a dictionary of lists, then clear all checkbuttons and whether the answer is wrong or right the program will continue on and I will eventually have a results screen but right now I am stuck on how to read the checkbutton on and off values. I am simply trying to print the selected veggies right now and cant figure it out.
I think it has to do with the fact they are in different methods and also the fact that they were added in a loop? I am not sure exactly but I know my code is trying to read the wrong thing and any help would be greeeeatly appreciated!
Sorry for the lengthy question I just thought it would be beneficial to give you as much info as possible to understand what I am trying to do..

from tkinter import *
class GUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.grid()
self.parent.title("Wahoos Menu Test")
self.create_buttons()
global count
count = -1
def create_buttons(self):
for r in range(20):
for c in range(14):
Label(self, text='',
borderwidth=0).grid(row=r,column=c)
B = Button(self, text ="Begin Exam", relief=RIDGE, fg="black", command= self.on_button_press).grid(row=19, column=7)
L = Label(self, text="What comes in the following", fg="blue").grid(row=6, column=0)
self.veg = ['Lettuce', 'Cabbage', 'Cheese', 'Ahee Rice', 'Brown Rice', 'Banzai Veg', 'Red Cabbage', 'Black Beans', 'Cajun White Beans']
self.vegboxes = []
self.opt = []
c = 1
for ve in self.veg:
c +=1
self.v = IntVar()
self.vegboxes.append(self.v)
vo = Checkbutton(self, text=ve, variable=self.v, onvalue=1, offvalue=0).grid(row=c, column=11, sticky=W)
def on_button_press(self):
global count
count = count + 1
menuItems = {'nft': ['cabbage', 'cheese', 'corn', 'nf', 'salsa'],
'nckt': ['lettuce', 'cheese', 'corn', 'nck', 'salsa']}
menu = ['blackened fish taco', 'wahoos chicken salad']
if count == len(menu):
C = Button(self, text =" Your Done! ", relief=RIDGE, fg="black").grid(row=19, column=7)
else:
m = Label(self, text=menu[count], fg="black").grid(row=7, column=0)
C = Button(self, text ="Submit and Continue", relief=RIDGE, fg="black", command= self.read_checks).grid(row=19, column=7)
def read_checks(self):
for v in self.veg:
if self.v == 1:
print(self.veg[v])
def main():
root = Tk()
app = GUI(root)
root.mainloop()
if __name__ == '__main__':
main()
You could create a dictionary and have each key be the Checkbutton’s label,
and have the value be the state “Control Variable”.
Then you would check the state with the Control Variable’s get() method as shown in the example below.
This approach allows you to create and analyze the Checkbuttons with one dictionary.
Note the use of items() in
for key, value in self.buttonDic.items():which is needed to prevent:
ValueError: too many values to unpackMore information on the Checkbutton widget and it’s variable can be found: here
I’m going to include my first attempt which was based on
the Checkbutton widget’s
onvalueandoffvalue,in case it helps someone understand Control Variables a bit more.