I am a rookie making a to-do list-GUI in Tkinter. Every task in the to-do list has a class attribute associated with it.
So a task is an object of Task:
and it has a value attribute self.value
.
I want to sort all the tasks displayed in the GUI based on the attribute self.value
. So for instance the task with the highest value is displayed on top of the list and the task with the lowest value is displayed at the bottom of the list.
Note that it is not the value that must be displayed in the gui but the attribute self.name
but it has to be displayed in an order based on the self.value
.
I only understand conceptually what to do (i think). Somethng like, i need to make a list based on the self.value
, then sort the "items" in the list from highest to lowest value, and then display the task.name
in the gui based on the ordering of self.value
.
Maybe I am retarded?
What would be the norm to do here?
EDIT
Code:
from tkinter import Tk, Frame, Button, Entry, Label, Toplevel
import tkinter.messagebox # Import the messagebox module
task_list = []
#value_list = []
class Task:
def __init__(self, n, h):
self.name = n
self.hours = h
def show_tasks():
task = task_list[-1]
print('\n')
print('_______________________')
print('\n')
print('Task:')
print(task.name)
print('\n')
print('Hours')
print(task.hours)
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
hourlabel = Label(taskwin, text='Whole hours \n required', font=('Roboto',10)).grid(column=1, row=2)
hour_entry = Entry(taskwin, width=4, justify='center')
hour_entry.grid(column=1, row=3)
def add_task():
if name_entry.get() != '':
task_list.append(Task(name_entry.get(), hour_entry.get()))
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=1, row=4)
def sort_tasks():
pass
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') # tkinter.Listbox(where it should go, height=x, width=xx)
listbox_tasks.pack()
#BUTTONS
New_Task_Button = Button(root, text='New Task', width=42, command=open_add_task)
New_Task_Button.pack()
root.mainloop()