Reputation: 2099
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
Upvotes: 0
Views: 1228
Reputation: 238159
I modified the code from the example adding the bits you have added. This is the full code:
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text="Done!", command=self.upload_cor)
b.pack()
table = tk.Frame(self)
table.pack(side="top", fill="both", expand=True)
data = (
(45417, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45418, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45419, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45420, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45421, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45422, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45423, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
)
self.widgets = {}
row = 0
for rowid, reviewer, task, num_seconds, start_time, end_time in (data):
row += 1
self.widgets[rowid] = {
"rowid": tk.Label(table, text=rowid),
"reviewer": tk.Label(table, text=reviewer),
"task": tk.Label(table, text=task),
"num_seconds_correction": tk.Entry(table),
"num_seconds": tk.Label(table, text=num_seconds),
"start_time": tk.Label(table, text=start_time),
"end_time": tk.Label(table, text=start_time),
"checkbox_var": tk.IntVar(),
"checkbox": tk.Checkbutton(table)
}
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")
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)
def upload_cor(self):
for rowid in sorted(self.widgets.keys()):
entry_widget = self.widgets[rowid]["num_seconds_correction"]
new_value = entry_widget.get()
print("%s: %s" % (rowid, new_value))
for rowid in sorted(self.widgets.keys()):
check_var = self.widgets[rowid]["checkbox_var"]
print(check_var.get())
if check_var.get():
print('Check button checked')
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
Specificlly have a look at upload_cor
. As you can see, instead of check_widget = self.widgets[rowid]["checkbox"]
I want to get the checkbox variable. I think this is the problem with your code. You get the widget, rather than a variable associated with the widget. Hope it helps.
Upvotes: 1