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.

2
  • 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? Commented Dec 13, 2012 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. Commented Dec 13, 2012 at 16:19

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

1

Use a treeview with checkboxes.

Treeview with checkboxes

How to create a tree view with checkboxes in Python

Comments

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.