4

I want to dynamically add ckeckboxes to my gui through a premade list. How would I populate the GUI with the names in my list? They all have the same type of functionality, so that should not be an issue.

user1876508
  • 12,864
  • 21
  • 68
  • 105
  • I'm a little confused by the problem description. "dynamically" to me means that the checkboxes will be added/removed on the fly during run time. But "premade" implies that the list of names is established before the program starts, and doesn't change. So which is it? – Kevin Dec 13 '12 at 15:31
  • I mean a list that is created during runtime when my csv parser populates the list with the names of the important columns. So it is a static list when the GUI is ran, but the csv parser dynamically imports the column names from an arbitrary csv file. – user1876508 Dec 13 '12 at 16:19

2 Answers2

4

If you want to populate your GUI with a premade list at startup:

from Tkinter import *

root = Tk()

premadeList = ["foo", "bar", "baz"]

for checkBoxName in premadeList:
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

root.mainloop()

If you want to dynamically populate your GUI with checkboxes at runtime:

import random
import string
from Tkinter import *

root = Tk()

def addCheckBox():
    checkBoxName = "".join(random.choice(string.letters) for _ in range(10))
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

b = Button(root, text="Add a checkbox", command=addCheckBox)
b.pack()

root.mainloop()

And of course, you can do both:

import random
import string
from Tkinter import *

root = Tk()

def addCheckBox():
    checkBoxName = "".join(random.choice(string.letters) for _ in range(10))
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

b = Button(root, text="Add a checkbox", command=addCheckBox)
b.pack()

premadeList = ["foo", "bar", "baz"]

for checkBoxName in premadeList:
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

root.mainloop()
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

Use a treeview with checkboxes.

Treeview with checkboxes

How to create a tree view with checkboxes in Python

Community
  • 1
  • 1
Brandon
  • 3,684
  • 1
  • 18
  • 25