mercredi 3 janvier 2018

How to create Checkboxes from list

I'm trying to make a very basic program right now that will show a window, and group of Checkboxs vertically in a labelframe. I want the Checkboxes to automatically generate based on the contents of a list. I found an entry from someone on here with a similar desire and copied his code, but I'm getting the error: "TypeError: list indices must be integers or slices, not str"

I'm new to Python, tried everything to correct this but coming up short. Any help would be great! Full code below:

from tkinter import *

#list to be used to make Checkboxes
pg = ["goal1","goal2"]


class yeargoals:
global pg
def __init__(self,master):
    self.master = master
    master.title("This Year's Goals")

    #Makes the label Fram I want the Checkboxes to go in
    self.LabelFramep= LabelFrame()
    self.LabelFramep.configure(width=210)

    #Makes the Checkboxs from the list above
    for goal in pg:
        pg[goal] = Variable()
        l = Checkbutton(self.LabelFramep, text=goal, variable=pg[goal])
        l.pack()


root = Tk()
Window = yeargoals(root)
root.mainloop()




Aucun commentaire:

Enregistrer un commentaire