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.
-
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– Kevin2012-12-13 15:31:50 +00:00Commented 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.user1876508– user18765082012-12-13 16:19:26 +00:00Commented Dec 13, 2012 at 16:19
Add a comment
|
2 Answers
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()
