I am trying to create checkboxes from a list. (biomelis_ = ['Arctic','Hill','Coast', 'Desert', 'Forest','Grassland', 'Mountain', 'Swamp', 'Underdark','Underwater', 'Urban' ]
).
I then want to check the boxes and press "go". I am trying to get the output to be a list. For example, if I check 'Arctic', 'Hill', and 'Coast', my output will be ['Arctic', 'Hill', 'Coast']
. I have been reading through the documentation but I am struggling with this.
Here is the full code:
from tkinter import *
biomelis_ = ['Arctic', 'Hill', 'Coast', 'Desert', 'Forest', 'Grassland', 'Mountain', 'Swamp', 'Underdark', 'Underwater',
'Urban']
root = Tk()
var = StringVar()
# var.set(biomelis_ [0])
def sel():
biome_lis_selection = var.get()
print(biome_lis_selection)
return [biome_lis_selection]
for item in range(len(biomelis_)):
l = Checkbutton(root, text=biomelis_[item], variable=var)
print("l = Checkbutton(root, text=" + str(biomelis_[item]) + ", variable=" + str(var))
l.pack(anchor='w')
def go(): ### runs tkinter
biomes_Checklist = sel()
b.config(state=NORMAL)
b.delete('1.0', END)
b.insert(INSERT, sel())
b.config(state=DISABLED)
## defines go button. This launches the def go() when the go button is pressed.
go_button = Button(root, text="Go!", width=10, command=go)
go_button.pack()
b = Text(root)
b.pack()
root.mainloop()
This is the part that I am struggling with-getting the selected items.
l = Checkbutton(root, text=biomelis_[item], variable=var)
This returns 'l' instead of individual items in the list. Also all of the checkboxes check and uncheck at once.