Reputation: 75
I am trying to make a todo-program. Every Task
has some attributes, one of them is a value based on some of the user input.
When you're adding a New Task there's an option to check all existing tasks which the new task could be somehow related to (e.g. maybe the New Task is to do the dishes and one of the existing tasks is to buy soap for it - so they're related somehow).
Here's a picture if it clarifies anything:
Let's say I have 3 boxes/existing tasks checked.
I want to retrieve each value attribute (val_var
in code) associated with each of the checked task buttons. The sum of all the checked task-values will then be an attribute, connectivity, of the New Task currently being added.
However, I am not sure how I can "grab" all the checkbutton-values of the buttons that have been checked even though it most likely is a trivial issue.
Simplified code:
from tkinter import Tk, Frame, Button, Entry, Label, Canvas, OptionMenu, Toplevel, Checkbutton
import tkinter.messagebox
task_list = []
task_types = ['Sparetime', 'School', 'Work']
class Task:
def __init__(self, n, h, v,):
self.name = n
self.hours = h
self.value = v
#self.connectivity = c
def show_tasks():
task = task_list[-1]
print('\n')
print('Value:')
print(task.value)
def open_add_task():
taskwin = Toplevel(root)
taskwin.focus_force()
#Name
titlelabel = Label(taskwin, text='Title task concisely:', font=('Roboto',11,'bold')).grid(column=1, row=0)
name_entry = Entry(taskwin, width=40, justify='center')
name_entry.grid(column=1, row=1)
#HOURS(required)
hourlabel = Label(taskwin, text='Whole hours \n required', font=('Roboto',10)).grid(column=1, row=16)
hour_entry = Entry(taskwin, width=4, justify='center')
hour_entry.grid(column=1, row=17)
#CONNECTIVITY
C_lab = Label(taskwin,text="Check tasks this task is related to").grid(column=1, row=18)
placement=19
for task in task_list:
Checkbutton(taskwin, text=(task.name)).grid(column=1, row=placement, sticky="w")
placement+=1
def add_task():
if name_entry.get() != '':
val_var = (int(hour_entry.get())/10)
task_list.append(Task(name_entry.get(), hour_entry.get(), val_var))
show_tasks()
listbox_tasks.insert(tkinter.END, name_entry.get())
name_entry.delete(0, tkinter.END)
taskwin.destroy()
else:
tkinter.messagebox.showwarning(title='Whoops', message='You must enter a task')
Add_button = Button(taskwin, text='Add', font=('Roboto',10), command=add_task).grid(column=2, row=placement, sticky="e")
placement+=1
root = Tk()
task_frame = Frame()
# Create UI
your_tasks_label = Label(root, text='THESE ARE YOUR TASKS:', font=('Roboto',10, 'bold'), justify='center')
your_tasks_label.pack()
listbox_tasks = tkinter.Listbox(root, height=10, width=50, font=('Roboto',10), justify='center')
listbox_tasks.pack()
#BUTTONS
New_Task_Button = Button(root, text='New Task', width=42, command=open_add_task)
New_Task_Button.pack()
root.mainloop()
Upvotes: 0
Views: 124
Reputation: 46669
You can use a list to hold tkinter DoubleVar
which is used in each task's Checkbutton
with its value
as the onvalue
option. Then you can sum all the values in the list of DoubleVar
to get the connectivity
.
Below is a modified example based on your code:
from tkinter import Tk, Frame, Button, Entry, Label, Canvas, OptionMenu, Toplevel, Checkbutton, DoubleVar
import tkinter.messagebox
task_list = []
task_types = ['Sparetime', 'School', 'Work']
class Task:
def __init__(self, n, h, v, c): # enable the "connectivity"
self.name = n
self.hours = h
self.value = v
self.connectivity = c
# added to show the task details
def __str__(self):
return f"{self.name}: hours={self.hours}, value={self.value}, connectivity={self.connectivity}"
def show_tasks():
task = task_list[-1]
print(task) # show the task details
def open_add_task():
taskwin = Toplevel(root)
taskwin.focus_force()
#Name
titlelabel = Label(taskwin, text='Title task concisely:', font=('Roboto',11,'bold')).grid(column=1, row=0)
name_entry = Entry(taskwin, width=40, justify='center')
name_entry.grid(column=1, row=1)
#HOURS(required)
hourlabel = Label(taskwin, text='Whole hours \n required', font=('Roboto',10)).grid(column=1, row=16)
hour_entry = Entry(taskwin, width=4, justify='center')
hour_entry.grid(column=1, row=17)
#CONNECTIVITY
C_lab = Label(taskwin,text="Check tasks this task is related to").grid(column=1, row=18)
placement=19
vars = [] # list to hold the DoubleVar used by Checkbutton
for task in task_list:
# add a DoubleVar to the list
vars.append(DoubleVar())
# use the task.value as the "onvalue" option
Checkbutton(taskwin, text=task.name, variable=vars[-1], onvalue=task.value, offvalue=0).grid(column=1, row=placement, sticky="w")
placement+=1
def add_task():
if name_entry.get() != '':
val_var = (int(hour_entry.get())/10)
# calculate the "connectivity" of the new task
connectivity = sum(v.get() for v in vars)
task_list.append(Task(name_entry.get(), hour_entry.get(), val_var, connectivity))
show_tasks()
listbox_tasks.insert(tkinter.END, name_entry.get())
name_entry.delete(0, tkinter.END)
taskwin.destroy()
else:
tkinter.messagebox.showwarning(title='Whoops', message='You must enter a task')
Add_button = Button(taskwin, text='Add', font=('Roboto',10), command=add_task).grid(column=2, row=placement, sticky="e")
placement+=1
root = Tk()
task_frame = Frame()
# Create UI
your_tasks_label = Label(root, text='THESE ARE YOUR TASKS:', font=('Roboto',10, 'bold'), justify='center')
your_tasks_label.pack()
listbox_tasks = tkinter.Listbox(root, height=10, width=50, font=('Roboto',10), justify='center')
listbox_tasks.pack()
#BUTTONS
New_Task_Button = Button(root, text='New Task', width=42, command=open_add_task)
New_Task_Button.pack()
root.mainloop()
Upvotes: 0