I have a modified version of the answer in this SO question, however the main structure remains the same. I was wondering how do I add check boxes to that dictionary to then iterate through the values of the check boxes? The check boxes in tkinter require a variable for which I'll eventually check, so I'm not sure of how to create the variables using this method.
How do I create the check boxes at the end of every row in the answer of the question I mention above?
I tried the following (last 2 lines):
self.widgets[rowid] = {
"rowid": ttk.Label(table, text=rowid, width=20, anchor=CENTER, relief=SUNKEN),
"reviewer": ttk.Label(table, text=reviewer, width=20, anchor=CENTER, relief=SUNKEN),
"task": ttk.Label(table, text=task, width=20, anchor=CENTER, relief=SUNKEN),
"num_seconds_correction": ttk.Entry(table),
"num_seconds": ttk.Label(table, text=num_seconds, width=20, anchor=CENTER, relief=SUNKEN),
"start_time": ttk.Label(table, text=start_time, width=20, anchor=CENTER, relief=SUNKEN),
"end_time": ttk.Label(table, text=start_time, width=20, anchor=CENTER, relief=SUNKEN),
"checkbox_var": IntVar(),
"checkbox": ttk.Checkbutton(table)
}
And in the part where the widgets are placed in the grid, I tried assigning the variable to the check box and then place it on the grid (last 2 lines):
#this just puts everything in the grid in the right place
self.widgets[rowid]["rowid"].grid(row=row, column=0, sticky="nsew")
self.widgets[rowid]["reviewer"].grid(row=row, column=1, sticky="nsew")
self.widgets[rowid]["task"].grid(row=row, column=2, sticky="nsew")
self.widgets[rowid]["num_seconds_correction"].grid(row=row, column=3, sticky="nsew")
self.widgets[rowid]["num_seconds"].grid(row=row, column=4, sticky="nsew")
self.widgets[rowid]["start_time"].grid(row=row, column=5, sticky="nsew")
self.widgets[rowid]["end_time"].grid(row=row, column=6, sticky="nsew")
self.widgets[rowid]["checkbox"].config(variable=(self.widgets[rowid]["checkbox_var"]))
self.widgets[rowid]["checkbox"].grid(row=row, column=7, sticky="nsew")
That makes the check boxes show up in the correct place. Then, when I iterate through the values to see which boxes are checked, even if some of them are checked, returns 0 for all of them:
for rowid in sorted(self.widgets.keys()):
check_widget = self.widgets[rowid]["checkbox"]
delete_value = check_widget.get()
print(delete_value)
This iteration returns only 0s even if the boxes are checked. What do I seem to be doing wrong here? Is it related to assigning the variables to the check boxes?
Then I tried something a little different:
self.check = IntVar()
self.widgets[rowid] = {
"rowid": ttk.Label(table, text=rowid, width=20, anchor=CENTER, relief=SUNKEN),
"reviewer": ttk.Label(table, text=reviewer, width=20, anchor=CENTER, relief=SUNKEN),
"task": ttk.Label(table, text=task, width=20, anchor=CENTER, relief=SUNKEN),
"num_seconds_correction": ttk.Entry(table),
"num_seconds": ttk.Label(table, text=num_seconds, width=20, anchor=CENTER, relief=SUNKEN),
"start_time": ttk.Label(table, text=start_time, width=20, anchor=CENTER, relief=SUNKEN),
"end_time": ttk.Label(table, text=start_time, width=20, anchor=CENTER, relief=SUNKEN),
"checkbox": ttk.Checkbutton(table, variable=self.check)
}
And then iterate over it:
for rowid in sorted(self.widgets.keys()):
print(self.check.get())
But it only prints 0s, any ideas?
Edit Here is the entire actual state of the class:
class Example(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
b = ttk.Button(self, text="Done!", command=self.upload_cor) #submit button
b.pack()
b2 = ttk.Button(self, text="Close", command=self.destroy_cor) #close button
b2.pack(side="right")
table = ttk.Frame(self)
table.pack(side="top", fill="both", expand=True)
data = results #results is the nested list with the query results of rows for one day
self.widgets = {}
row = 0
total=0 #this will keep track of the total of hours for that day
#this iteration creates all the labels and positions them
for rowid, reviewer, task, num_seconds, start_time, end_time in (data):
row += 1
self.check = BooleanVar()
#this is a dictionary, this makes it easier keep track of the values inside
#text boxes when they're changed
self.widgets[rowid] = {
"rowid": ttk.Label(table, text=rowid, width=20, anchor=CENTER, relief=SUNKEN),
"reviewer": ttk.Label(table, text=reviewer, width=20, anchor=CENTER, relief=SUNKEN),
"task": ttk.Label(table, text=task, width=20, anchor=CENTER, relief=SUNKEN),
"num_seconds_correction": ttk.Entry(table),
"num_seconds": ttk.Label(table, text=num_seconds, width=20, anchor=CENTER, relief=SUNKEN),
"start_time": ttk.Label(table, text=start_time, width=20, anchor=CENTER, relief=SUNKEN),
"end_time": ttk.Label(table, text=start_time, width=20, anchor=CENTER, relief=SUNKEN),
"checkbox": ttk.Checkbutton(table, variable=self.check)
}
total = total + int(num_seconds)
#this just puts everything in the grid in the right place
self.widgets[rowid]["rowid"].grid(row=row, column=0, sticky="nsew")
self.widgets[rowid]["reviewer"].grid(row=row, column=1, sticky="nsew")
self.widgets[rowid]["task"].grid(row=row, column=2, sticky="nsew")
self.widgets[rowid]["num_seconds_correction"].grid(row=row, column=3, sticky="nsew")
self.widgets[rowid]["num_seconds"].grid(row=row, column=4, sticky="nsew")
self.widgets[rowid]["start_time"].grid(row=row, column=5, sticky="nsew")
self.widgets[rowid]["end_time"].grid(row=row, column=6, sticky="nsew")
#self.widgets[rowid]["checkbox"].config(variable=(self.widgets[rowid]["checkbox_var"]))
self.widgets[rowid]["checkbox"].grid(row=row, column=7, sticky="nsew")
ttk.Label(table, text='Total:',width=20, anchor=E, relief=SUNKEN).grid(row=row+1, column=3) #this 2 display the total hours
ttk.Label(table, text=total, width=20, anchor=CENTER, relief=SUNKEN).grid(row=row+1, column=4) #spent on the selected day
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
# invisible row after last row gets all extra space
table.grid_rowconfigure(row+1, weight=1)
globals().update(locals())
#########################
#uploads the new values if they
#were changed
def upload_cor(self):
globals().update(locals())
for rowid in sorted(self.widgets.keys()):
print(self.check)
#here is where I need something to see if the boxes are checked