0

我正在尝试制作一个待办事项程序。每个Task都有一些属性,其中一个是基于某些用户输入的值。当您添加新任务时,可以选择检查与新任务可能相关的所有现有任务(例如,新任务可能是洗碗,而现有任务之一是为其购买肥皂 - 所以它们以某种方式相关)。

如果它澄清了任何事情,这是一张图片:

UI 的图片,简化

假设我检查了 3 个框/现有任务。我想检索val_var与每个选中的任务按钮关联的每个值属性(在代码中)。然后,所有检查的任务值的总和将成为当前添加的新任务的属性连接性。

但是,我不确定如何“获取”已检查按钮的所有检查按钮值,即使这很可能是一个微不足道的问题。

简化代码:

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()

4

1 回答 1

0

您可以使用列表来保存DoubleVar每个任务中使用的 tkinter,Checkbutton并将其value作为onvalue选项。然后你可以将列表中的所有值相加DoubleVar得到connectivity.

以下是基于您的代码的修改示例:

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()
于 2021-05-26T03:33:57.687 回答