Weston Sapusek
Weston Sapusek

Reputation: 95

generating dynamic number of checkboxes in tkinter

I'm writing some code to transfer files off of my C drive and using it as a learning exercise with tkinter. Currently I have it set up to read all the files out of my Python folder and make a checkbox for each one, and it can successfully display that list. However when you click one of the checkboxes, they all flip on or off. What part of the Checkbutton definition controls this and how would I go about changing it while still being able to generate as many checkboxes as needed? I've included the relevant code below.

var = []
x = 0
while x <= 10000:
    var.append(0)
    x += 1
path = 'C:\\Users\\ebonh\\Documents\\PythonStuff'
files = []
for r, d, f in os.walk(path):
    for file in f:
        files.append(os.path.join(r, file))
Label(master, text="Transfer these files to D Drive:").grid(row=0, sticky=W)
x = 0
for f in files:
    Checkbutton(master, text=f, variable=var[x]).grid(row=(x+1), sticky=W)
    x += 1

Upvotes: 0

Views: 460

Answers (1)

lidrariel
lidrariel

Reputation: 79

The tkinter checkbox expects an IntVar variable, so fill your list with those:

while x <= 10000:
    var.append(IntVar(0))
    x += 1

Then they are not checked together anymore.

Upvotes: 1

Related Questions