For some reason, my code for a Checkbutton variable does not change work when it is inside of a function, however, it works when outside of a function and in its own script. Here is the code for outside a function
from tkinter import *
basket = ['apple','banana','orange']
win2 = Tk()
buttons = []
variables = []
for fruit in basket:
variable = BooleanVar()
checkbutton = Checkbutton(win2, text = fruit,variable = variable).pack()
variables.append(variable)
buttons.append(checkbutton)
Button(win2, text='Quit', command=win2.destroy).pack()
Button(win2, text='Peek', command=allstates).pack()
mainloop()
Here is the code when defined inside of a function
from tkinter import *
def selectfruit():
def allstates():
for variable in variables:
print(variable.get())
basket = ['apple','banana','orange']
win2 = Tk()
print(stls)
buttons = []
variables = []
for fruit in basket:
variable = BooleanVar()
checkbutton = Checkbutton(win2, text = fruit,variable = variable).pack(side = TOP)
variables.append(variable)
buttons.append(checkbutton)
Button(win2, text='Quit', command=win2.destroy).pack()
Button(win2, text='Peek', command=allstates).pack()
For the top bit of code, when I check a box the variables go to True. For the bottom one, they stay at False regardless of what I do.