How to Build a To-Do List App with Python and Kivy
In this tutorial, you'll learn how to create a simple, responsive To-Do List application using Python and Kivy. This project is great for beginners and will introduce you to key concepts in building cross-platform desktop and mobile applications with Kivy.
We will build the entire UI and logic in one Python file.
Step 1: Installing Kivy
First, install Kivy using pip:
pip install kivy
Make sure you are using a virtual environment if you're working on a shared system or project.
Step 2: Creating the Application Layout
We will use Kivy's BoxLayout to arrange the widgets vertically. The UI will include:
- A text input field for typing new tasks
- A button to add the typed task
- A scrollable list of tasks
- A delete button for each task
This layout allows us to create a basic yet functional interface for managing tasks.
Step 3: Writing the Python Code
Below is the complete Python code for the application. It defines the layout and behavior in one script.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
class ToDoListApp(App):
def build(self):
self.task_list_layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
self.task_list_layout.bind(minimum_height=self.task_list_layout.setter('height'))
root_layout = BoxLayout(orientation='vertical', padding=20, spacing=10)
input_layout = BoxLayout(size_hint_y=None, height=40, spacing=10)
self.task_input = TextInput(hint_text='Enter a new task')
add_button = Button(text='Add')
add_button.bind(on_press=self.add_task)
input_layout.add_widget(self.task_input)
input_layout.add_widget(add_button)
scroll_view = ScrollView()
scroll_view.add_widget(self.task_list_layout)
root_layout.add_widget(input_layout)
root_layout.add_widget(scroll_view)
return root_layout
def add_task(self, instance):
task_text = self.task_input.text.strip()
if task_text:
task_box = BoxLayout(size_hint_y=None, height=40, spacing=10)
task_label = Label(text=task_text, halign='left', valign='middle')
task_label.bind(size=task_label.setter('text_size'))
delete_button = Button(text='Delete', size_hint_x=None, width=80)
delete_button.bind(on_press=lambda x: self.remove_task(task_box))
task_box.add_widget(task_label)
task_box.add_widget(delete_button)
self.task_list_layout.add_widget(task_box)
self.task_input.text = ''
else:
print("You must enter a task.")
def remove_task(self, task_widget):
self.task_list_layout.remove_widget(task_widget)
if __name__ == '__main__':
ToDoListApp().run()
Step 4: Running the App
Save the file as todo_kivy.py and run it using the command below:
python todo_kivy.py
A window will open where you can add tasks, view them in a scrollable list, and remove any task by clicking its Delete button.
Conclusion
You have now created a functional To-Do List application using Python and Kivy. This project demonstrates how to build user interfaces, handle events, and manage dynamic content, all in a single Python script.
This app works on desktop platforms and can be easily adapted for mobile devices using tools like Buildozer or Pyjnius.
For more Kivy projects and tutorials, explore the rest of our blog.