My end goal is to have a series of Checkbox's automatically created and updated based on a list, with the list being updated via an entry page. I've achieved all these things, except, when the list is updated the corresponding Checkbox's don't.
I've tried to include only the parts of the code that are relevant, but if more or the entire thing is needed please let me know and I'll post more.
Here is where the window, list, frames, buttons, and the iteration that creates the list is done:
from tkinter import *
pg = ["goal1","goal2"]
pgtotal=1
psum=len(pg)
class yeargoals:
global pg, hg, fg, rg, rgtotal
def __init__(self,master):
self.master = master
master.title("This Year's Goals")
self.buttonframe = Frame(root)
self.buttonframe.pack(side=TOP, padx = 150, fill=BOTH)
self.home = Button(self.buttonframe, text="Home Page")
self.home.grid(row=1, column=1, padx=10)
self.enter = Button(self.buttonframe, text="Goal Entry", command=winenter)
self.enter.grid(row=1, column=2, padx=10)
self.finalize = Button(self.buttonframe, text="Finalize for Year")
self.finalize.grid(row=1, column=3, padx=10)
self.dashboard = Button(self.buttonframe, text="Goal Dashboard")
self.dashboard.grid(row=1,column=4, padx=10)
self.goalframe = Frame(root)
self.goalframe.pack(side=TOP, padx=150, pady=50, fill=BOTH, expand = True)
#Makes the label Fram I want the Checkboxes to go in
self.LabelFramep= LabelFrame(self.goalframe,text="Professional Goals")
self.LabelFramep.pack(side=LEFT, padx=10, anchor = N, fill=BOTH, expand = True)
#Makes the from the list above
for goal in pg:
l = Checkbutton(self.LabelFramep, text=goal, variable=Variable())
l.config(font=("Courier",12))
l.grid(sticky=W)
self.ptotal=Label(self.LabelFramep,text="Progress so far: "+str(pgtotal)+"/"+str(psum))
self.ptotal.config(font=("Courier",12))
self.ptotal.grid(sticky=W)
self.pper=Label(self.LabelFramep, text=str(round((pgtotal/psum)*100))+"% Complete")
self.pper.config(font=("Courier",12))
self.pper.grid(sticky=W)
Here is the code that creates the new Toplevel window with the entry fields that adds goals to the list:
def winenter():
global pg
winenter = Toplevel(root)
options = ["Professional", "Health", "Financial", "Reward Items"]
variable = StringVar(winenter)
variable.set(options[0])
#Title of entry section
t1 = Label(winenter, text="New Goal Entry")
t1.grid(row=0, column=1, columnspan=2)
#dropdown menu
d = OptionMenu(winenter, variable, *options)
d.grid(row=1, column=2)
#entry fields
e1 = Entry(winenter)
e1.grid(row=2, column=2, padx = 10, pady=5)
#Label for entry fields
l1 = Label(winenter, text="Goal Number 1")
l1.grid(row=2, column=1)
def enter():
global pg
if variable.get() == "Professional":
pg.append(e1.get())
#Goal entry execute button
b = Button(winenter, text="Enter Goals", command=enter)
b.grid(row=7, column = 1)
And the end of the code to create the root window:
root = Tk()
Window = yeargoals(root)
root.mainloop()
When you enter text into the entry field and hit the enter button the list IS updated, I know this by having texted it by printing the list before and after entry. However despite a goal being added when I close then entry window there isn't a new corresponding Checkbox.
I'm assuming I need some kind of update or refresh function, but despite attempts have been unsuccessful. Any help would be great.
Aucun commentaire:
Enregistrer un commentaire