Dynamically add checkboxes with tkinter in python 2.7

I want to dynamically add ckeckboxes to my gui through a list of prebuilt ones. How to populate GUI with names in my list? They all have the same type of functionality, so this should not be a problem.

+4
source share
2 answers

If you want to populate your GUI with a ready-made 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 flags 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() 
+3
source

Use a tree structure with checkboxes.

Treeview with checkboxes

How to create checkbox tree view in Python

+1
source

All Articles