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