简体   繁体   English

从检查按钮检索属性 - Tkinter,检查按钮,python

[英]Retrieving attribute from checkbuttons - Tkinter, checkbuttons, python

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.每个Task都有一些属性,其中一个是基于某些用户输入的 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 (eg 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).当您添加新任务时,有一个选项可以检查新任务可能与之相关的所有现有任务(例如,新任务可能是洗碗,而现有任务之一是为其购买 soap - 所以它们以某种方式相关)。

Here's a picture if it clarifies anything:如果它澄清了任何事情,这是一张图片:

UI 的图片,简化

Let's say I have 3 boxes/existing tasks checked.假设我检查了 3 个框/现有任务。 I want to retrieve each value attribute ( val_var in code) associated with each of the checked task buttons.我想检索与每个选中的任务按钮关联的每个值属性(代码中的val_var )。 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()

You can use a list to hold tkinter DoubleVar which is used in each task's Checkbutton with its value as the onvalue option.您可以使用列表来保存 tkinter DoubleVar ,它在每个任务的Checkbutton中使用,其value作为onvalue选项。 Then you can sum all the values in the list of DoubleVar to get the connectivity .然后您可以对DoubleVar列表中的所有值求和以获得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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM