Table of Contents
Introduction
In this tutorial, we will be creating a simple To-Do List application using Python. By the end of this tutorial, you will have a functional application that allows you to add, remove, and view tasks on your to-do list. This tutorial is suitable for beginners and assumes basic knowledge of Python programming.
Prerequisites
Before starting this tutorial, you should have the following:
- Basic knowledge of Python programming
- Python installed on your machine
Setup
To get started, create a new directory for your project. Open your preferred text editor or integrated development environment (IDE) and create a new Python file named todo.py
.
Creating the User Interface
We will be using the tkinter
library to create a graphical user interface (GUI) for our To-Do List application. Start by importing the tkinter
module:
python
import tkinter as tk
Next, let’s create a MainWindow
class which will serve as the main window for our application. Add the following code:
```python
class MainWindow(tk.Tk):
def init(self):
super().init()
self.title(“To-Do List Application”)
self.tasks = []
self.task_entry = tk.Entry(self)
self.task_entry.pack()
self.add_button = tk.Button(self, text="Add Task", command=self.add_task)
self.add_button.pack()
self.task_listbox = tk.Listbox(self)
self.task_listbox.pack()
self.remove_button = tk.Button(self, text="Remove Task", command=self.remove_task)
self.remove_button.pack()
self.refresh_tasks()
def add_task(self):
task = self.task_entry.get()
if task:
self.tasks.append(task)
self.refresh_tasks()
self.task_entry.delete(0, tk.END)
def remove_task(self):
selection = self.task_listbox.curselection()
if selection:
index = selection[0]
del self.tasks[index]
self.refresh_tasks()
def refresh_tasks(self):
self.task_listbox.delete(0, tk.END)
for task in self.tasks:
self.task_listbox.insert(tk.END, task)
if __name__ == "__main__":
app = MainWindow()
app.mainloop()
``` Let's go through the code step by step.
- We create a class
MainWindow
that inherits fromtkinter
’sTk
class. We initialize the main window and set the title to “To-Do List Application”. - Inside the
MainWindow
class, we define the__init__
method which sets up the initial state of the application. We create an empty listself.tasks
to store the tasks. - We create a
tk.Entry
widget namedself.task_entry
to allow the user to enter a new task. It is then packed onto the window using thepack()
method. - Similarly, we create a
tk.Button
widget namedself.add_button
with the text “Add Task” and a command to call theadd_task
method when clicked. We pack this button onto the window. - Next, we create a
tk.Listbox
widget namedself.task_listbox
to display the list of tasks. It is also packed onto the window. - Finally, we create a
tk.Button
widget namedself.remove_button
with the text “Remove Task” and a command to call theremove_task
method when clicked. We pack this button onto the window as well. - We also call the
refresh_tasks
method to update the task list on the GUI.
The add_task
method retrieves the task from the entry widget, adds it to the list of tasks, refreshes the task list, and clears the entry widget.
The remove_task
method gets the current selection from the task listbox, deletes the corresponding task from the list, and refreshes the task list.
The refresh_tasks
method clears the task listbox and inserts each task from the list into the box.
Finally, we create an instance of the MainWindow
class and start the application’s main event loop by calling the mainloop
method.
Save the file and run it using the command python todo.py
in the command prompt or terminal. You should see a window with an entry field, two buttons, and a listbox.
Adding Functionality
The basic functionality of the application is complete, but there are a few improvements we can make. Let’s add keyboard shortcuts, task selection, and confirmation dialogs for task removal.
First, let’s add keyboard shortcuts to our buttons. Update the button creation lines as follows:
python
self.add_button = tk.Button(self, text="Add Task", command=self.add_task, underline=0)
self.remove_button = tk.Button(self, text="Remove Task", command=self.remove_task, underline=0)
We added the underline
parameter and set it to 0
for both buttons. This means that when the user presses Alt+A
or Alt+R
, it will trigger the respective button’s action.
Next, let’s allow the user to select and deselect tasks in the listbox. We will bind the <<ListboxSelect>>
event to a method called select_task
. Add the following lines inside the __init__
method, before the call to self.refresh_tasks()
:
python
self.task_listbox.bind("<<ListboxSelect>>", self.select_task)
self.selected_task = None
Now, let’s implement the select_task
method:
python
def select_task(self, event):
selected_index = self.task_listbox.curselection()
if selected_index:
index = selected_index[0]
self.selected_task = self.tasks[index]
This method gets the index of the selected item in the task listbox and sets the selected_task
attribute to the corresponding task.
Finally, let’s add a confirmation dialog when removing a task. Update the remove_task
method as follows:
python
def remove_task(self):
if self.selected_task:
confirm = tk.messagebox.askyesno("Confirm", f"Are you sure you want to remove task '{self.selected_task}'?")
if confirm:
del self.tasks[self.task_listbox.curselection()[0]]
self.refresh_tasks()
self.selected_task = None
else:
tk.messagebox.showinfo("Error", "No task selected.")
We use the askyesno
method from the messagebox
module to display a confirmation dialog. If the user confirms the removal, the selected task is deleted from the list, the task listbox is refreshed, and the selected_task
attribute is reset to None
. If no task is selected when the removal button is clicked, an error message is shown.
Save the file and run the application again. You should now be able to use keyboard shortcuts, select tasks in the listbox, and receive a confirmation dialog when removing tasks.
Conclusion
In this tutorial, we have created a basic To-Do List application using Python and the tkinter
library. We covered the steps for creating a graphical user interface, adding functionality to handle task addition and removal, and improving the user experience with keyboard shortcuts and selection. You can further enhance the application by adding features like task editing, saving tasks to a file, or adding deadlines.
Feel free to explore and customize the application to suit your needs. Happy coding!