0

Guys I am a newbie in Programming. I started learning coding (Python) a few days back. I watched a youtube video and made a To Do List. But there is one thing that wasn't explained in the video that I would like to ask some of you who know Python more than me.

In the code below, I do not know how to save my changes.

Lets say I create my tasks that I want to do, but then once I close the program all my details are lost. Is it possible to save the data once I open the .py file again?

Thanks for the help guys!

import tkinter
from tkinter import messagebox
from tkinter import font

#Create Root Window
root = tkinter.Tk()

#Change root window background color
root.configure(bg='white')

# define font
my_btn_font = font.Font(size=11,family='great vibes')
my_listbox_font = font.Font(size=10,family='great vibes')
my_textbox_font = font.Font(size=15,family='great vibes')

#Change the titel
root.title('My Daily To Do List')

#Change window size
root.geometry('335x350')
root.resizable(False, False)

#Create an empty list
tasks = []

#For testing purposes use a default list
#tasks = ['Work', 'Chill','Relax']

#********************************************************************
#Defining all functions here

def update_listbox():
    #Clear current list
    clear_listbox()
    #Populate the listbox
    for task in tasks:
        listbox_tasks.insert('end', task)

def clear_listbox():
    listbox_tasks.delete(0, 'end')

def add_task():
    #Get the task to add
    task = text_input.get()
    #Make sure the task is not empty
    if task !="":    
        #Append to the list
        tasks.append(task)
        #Update the listbox
        update_listbox()
    else:
        messagebox.showinfo('Info', 'Please enter a task')
    text_input.delete(0, 'end')    
def delete_all():
    confirmed = messagebox.askyesno('Please confirm:', 'Do you really want to Delete all?')
    if confirmed == True:
        #Making the delete all list global
        global tasks
        #Clear the tasks list
        tasks = []
        #Update the listbox
        update_listbox()

def del_one():
    #Get the text of the currently selected item
    task = listbox_tasks.get('active')
    #Confirm it is in the list
    if task in tasks:
        tasks.remove(task)
    #Update the listbox
    update_listbox()

def sort_asc():
    #Sort the list
    tasks.sort()
    #Update the listbox
    update_listbox()

def sort_des():
    #Sort the list
    tasks.sort()
    #Reverse the list
    tasks.reverse()
    #Update the listbox
    update_listbox()

def show_number_of_tasks():
    #Get the number of tasks
    number_of_tasks = len(tasks)
    #Create and format the message
    msg = 'Number of tasks: {}'.format(number_of_tasks)
    #Display the message
    lbl_display['text']=msg

#***********************************************************************
#Adding Listbox, Input box & Title

# Creating Title
lbl_title = tkinter.Label(root, text='To-Do-List', bg='white')
lbl_title.grid(row=0,column=0)
lbl_title['font'] = my_btn_font 

lbl_display = tkinter.Label(root, text='', bg='white')
lbl_display.grid(row=0,column=1)
lbl_title['font'] = my_btn_font 

#Creating Text input box
text_input = tkinter.Entry(root, bd=4, font=my_textbox_font)
text_input.grid(row=1,column=1)

#******************************************************************
#Adding buttons and starting main loop

#Creating all the buttons
btn_add_task = tkinter.Button(root, text='Add Task', fg='gray4', bg='white', command=add_task, width=9)
btn_add_task.grid(row=1,column=0)
btn_add_task['font'] = my_btn_font 

btn_delete_all = tkinter.Button(root, text='Delete All', fg='gray4', bg='white', command=delete_all, width=9)
btn_delete_all.grid(row=2,column=0)
btn_delete_all['font'] = my_btn_font 

btn_del_one = tkinter.Button(root, text='Delete', fg='gray4', bg='white', command=del_one, width=9)
btn_del_one.grid(row=3,column=0)
btn_del_one['font'] = my_btn_font 

btn_sort_asc = tkinter.Button(root, text='Sort(ASC)', fg='gray4', bg='white', command=sort_asc, width=9)
btn_sort_asc.grid(row=4,column=0)
btn_sort_asc['font'] = my_btn_font 

btn_sort_des = tkinter.Button(root, text='Sort(DSC)', fg='gray4', bg='white', command=sort_des, width=9)
btn_sort_des.grid(row=5,column=0)
btn_sort_des['font'] = my_btn_font 

btn_number_of_tasks = tkinter.Button(root, text='No. of Tasks', fg='gray4', bg='white', command=show_number_of_tasks, width=9)
btn_number_of_tasks.grid(row=6,column=0)
btn_number_of_tasks['font'] = my_btn_font 

btn_exit = tkinter.Button(root, text='Exit', fg='gray4', bg='white', command=exit, width=9)
btn_exit.grid(row=7,column=0)
btn_exit['font'] = my_btn_font 

listbox_tasks = tkinter.Listbox(root, bd=4, height=10, width=20, font='my_listbox_font')
listbox_tasks.grid(row=2,column=1, rowspan=7)

#Start the main events loop
root.mainloop()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [Saving the state of a program to allow it to be resumed](https://stackoverflow.com/questions/5568904/saving-the-state-of-a-program-to-allow-it-to-be-resumed) also helpful: https://stackoverflow.com/questions/17179222/python-saving-data-outside-of-program – Tomerikoo May 28 '20 at 08:48
  • Anyway, you should probably read about [ask] and how to provide a [mre]. As it seems from your question, all this (huge) code that you posted is basically not even related to the question. You don't have a problem with the code itself, you have something you want to **add** to your code – Tomerikoo May 28 '20 at 08:51
  • 1
    In the tutorial: [Reading and Writing Files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) – Matthias May 28 '20 at 08:57
  • Hey Tomerikoo, it does answer my questions, but I do not know how to implement it in my Python code. Neither Shelve nor Pickle. I have only been programming since 5 days now... This is my first question, will keep it in mind. – Captain Tj May 28 '20 at 09:04
  • If you want your data to be persistent, you have two choices. One is to use files and write your to-do list out to a file, which can be read when you open your application. The other is to use a database which is definitely a nicer approach although it is more advanced for a newbie. Either way you have to take care of the add/update/del methods to work with your file/db to make changes that persist over time. – Dominik Érsek May 28 '20 at 09:06

0 Answers0