Python To-Do List — From Lines to Lists
What is a to-do list?
A To-do list is a compilation of essential tasks an individual needs to complete. The list arranges these tasks in order of significance, with the most crucial tasks positioned at the top and less important ones further down. This list serves as a crucial tool for keeping track of tasks that require timely completion.
Although traditional To-do lists were often handwritten on paper, modern times offer digital applications that store To-do lists electronically. Today, we’ll create a similar To-do list application using Python.
About Python To Do List Project:
In this Python project, we’ll create a To-do list application in Python using the tkinter library to create its user interface. The application will feature “Add” and “Remove” buttons. The “Add” button adds tasks to the list, and the “Remove” button allows users to delete specific tasks. To develop this Python to-do list project, a basic familiarity with Python and Tkinter is sufficient.
Prerequisites for Python To Do List:
- Initially, we’ll use the pip installer to install the necessary library on our system.
- This project necessitates a solid understanding of Python and proficiency in using tkinter to develop the GUI for the application.
pip install tkinter
Importing the tkinter Library:
from tkinter import *
tkinter- This library will help us in creating a GUI window for our app.
Initializing GUI Window- We will initiate the GUI window for the application.
root = Tk()
root.geometry('350x450')
root.resizable(False,False)
root.title("To-do List by Pythonflood")
root.config(bg='#BCD2EE')
root- It is the name of our GUI window.
Tk()- Initialised tkinter which means GUI window created.
geometry()- This method provides the length and breadth to the GUI window.
resizeable()- This method allows the window to change its size as per user need.
title()- This method gives title to the window
confg()- This method sets the configuration of the window.
Creating Label, Frame, Listbox and Scrollbar- We will create the frame for the labels and listbox. We will also create a scrollbar to scroll items in the listbox.
Label(root,text="To-do list by Pythonflood",font=('Arial,bold',20),width=30,bg='#6E7B8B',fg='White').pack(pady=5)
f=Frame(root,width=300, height=350)
f.place(x=15,y=50)
lbox=Listbox(f,font=('Arial,bold',15),width=25,height=12)
lbox.pack(side=LEFT,fill= BOTH)
scrollb=Scrollbar(f, orient=VERTICAL)
scrollb.config(command=lbox.yview)
scrollb.pack(side=RIGHT, fill=Y)
lbox.config(yscrollcommand=scrollb.set)
with open ('lbox.txt', 'r+') as task_list:
for task in task_list:
lbox.insert(END, task)
task_list.close()
Frame()- It is like a container, which is used for positioning the widgets.
Listbox()- The Listbox widget is employed to present a list of items to the user.
side()- This will place the widget on Left, Right, Top and Bottom.
yview()- This will make the scrollbar move vertically.
Scrollbar()- The Scrollbar Widget is used to slide down the content of listbox.
yscrollcommand()- It will move the widget vertically.
Label()- It is used to display one line or more than one line of text.
text- It is used to display text on label.
font- It refers to a specific text style in which the font is displayed.
bg- It is the background Colour of the label.
pack()- This displays the widgets in the GUI window.
place()- It is used to set the position.
Creating an Entry field and Buttons- We will create an entry field widget to collect user input and a button widget to enable adding and removing items from the list.
en=Entry(root,width=30)
en.place(x=75,y=360)
bn1=Button(root,text="Add",font=('Arial,bold',15),bg='#6E7B8B',fg='White',padx=10,command= lambda: add(en,lbox))
bn1.place(x=60,y=390)
bn2=Button(root,text="Remove",font=('Arial,bold',15),bg='#6E7B8B',fg='White',command= lambda: remove(lbox) )
bn2.place(x=160,y=390)
root.update()
root.mainloop()
Entry()- In this widget, users will input random words of their choosing.
Button()- It is a button used to display on our window.
command- It is used as a function of a button when it is clicked.
place()- It is used to set the position.
root.mainloop()- This method executes the desired operations within an application and concludes the mainloop.
Defining Add task Function-
def add(entry: Entry, listbox: Listbox):
new_item=entry.get()
listbox.insert(END, new_item)
with open('lbox.txt', 'a') as task_file:
task_file.write(f'\n{new_item}')
In this section, we’ve defined an “add” function aimed at appending items to the listbox, positioning them at the list’s tail end.
Subsequently, the added items are preserved in a text file through utilisation of the `write` method.
get()- It retrieves the value entered in the Entry Field.
insert()- This method will insert the task in the listbox.
write()- It will write the tasks in the file.
open(‘a’)- This is used for appending the task to the file.
Defining Remove task Function-
def remove(listbox: Listbox):
listbox.delete(ACTIVE)
with open('lbox.txt', 'r+') as task_file:
lines = task_file.readlines()
task_file.truncate()
for line in lines:
if listbox.get(ACTIVE) == line [:-2]:
lines.remove(line)
task_file.write(line)
task_file.close()
Now, we’ve established a “remove” function to facilitate the elimination of items from the list. The designated item is extracted from the list by employing the `listbox.delete(ACTIVE)` method.
Following this, the file is accessed in read mode through the `with open` construct.
The `for` loop is used to ascertain whether the chosen list item corresponds to a line in the text file. If a match is identified, the line is removed from the file. The updated content is then written back into the file using the `write` method, and the process concludes by closing the file through the `task_file.close()` method.
delete()- This will delete the selected task from Listbox.
open(‘r+’)- This will open the file in reading mode.
readlines()- It will store all the lines in the file.
truncate()- It is used to reduce the size of the file.
remove()- It will remove the selected lines.
close()- This will close the file.
Python To Do List Output:
Conclusion:
Great job! We’ve effectively developed a To-do list application in Python using the tkinter library to craft the graphical user interface (GUI). You’re now equipped to build your own personalised To-do list application and tailor it to your preferences.