In today’s article, we are going to create a simple to-do list using the tkinter
library in Python. This code will enable the user to add or delete tasks, thanks to a graphical user interface (GUI).
Let’s get started!
Related articles:
Table of Contents
- Necessary Libraries
- Imports
- Functions
- Creating the Main Window
- User Interface Elements
- Frame For Listbox and Scrollbar
- Listbox and Canvas
- Scrollbar
- Delete Task Button
- Main Event Loop
- Example
- Full Code
Necessary Libraries
To get this code up and running, make sure to install the tkinter library via the terminal or your command prompt.
$ pip install tk
Imports
import tkinter as tk
from tkinter import messagebox
First, we start by importing the tkinter
library, which creates a graphical user interface (GUI), and from tkinter
we import the messagebox
module that displays message boxes.
Functions
def add_task():
task = entry.get()
if task:
listbox.insert(tk.END, task)
entry.delete(0, tk.END)
else:
messagebox.showwarning("Warning", "Please enter a task.")
def delete_task():
try:
selected_task = listbox.curselection()[0]
listbox.delete(selected_task)
except IndexError:
messagebox.showwarning("Warning", "Please select a task to delete.")
The code presented here defines two functions, once the user puts his task in the entry field the first function adds it to the listbox
while clearing the entry field, and if no task is entered in the entry field, an error message pops up.
The second one’s job is to delete the task selected from the listbox
because if no task is selected for deletion, an error message appears.
Creating the Main Window
root = tk.Tk()
root.title("To-Do List - The Pycodes")
root.geometry("400x600")
root.configure(bg="black")
tk.Label(root,text="Today's To Do List",font="arial 20 bold",bg="black",fg="red").place(x=70,y=20)
root.resizable(False,False)
Here, we created the main window, gave it a title and set its size, background color, and disabled its resizing.
User Interface Elements
entry = tk.Entry(root, width=40,font="arial 12")
entry.pack(pady=100)
# Create a button to add tasks
add_button = tk.Button(root, text="Add Task", command=add_task)
add_button.place(x=175,y=150)
After that, we create an entry field where the user can type the tasks and create a button that, once clicked, triggers the add_task
function.
Frame For Listbox and Scrollbar
frame = tk.Frame(root,bd=3,width=100)
frame.pack()
Next, we make a container for the listbox
and scrollbar
.
Listbox and Canvas
# Create a listbox to display tasks
listbox = tk.Listbox(frame, width=40,height=15, selectmode=tk.SINGLE,bg="grey",fg="white",font="arial 11")
listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Create a Canvas widget to contain the listbox
canvas = tk.Canvas(frame, bg="grey")
canvas.pack(side=tk.RIGHT, fill=tk.BOTH)
The above code creates a listbox
with specific width, height, and color, and also creates a canvas
to contain the listbox
for scrolling.
Scrollbar
# Create a scrollbar for the listbox
scrollbar = tk.Scrollbar(canvas,orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)
# Configure the listbox to use the scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
Following that, we create a vertical scrollbar in the listbox to allow scrolling up and down.
Delete Task Button
# Create a button to delete selected tasks
delete_button = tk.Button(root, text="Delete Task", command=delete_task)
delete_button.place(x=170,y=530)
Then we create a button once clicked triggers the delete_task
function.
Main Event Loop
# Start the main event loop
root.mainloop()
Lastly, this part starts the main event loop making the main window in a loop allowing the user to utilize the GUI elements.
Example
Full Code
import tkinter as tk
from tkinter import messagebox
def add_task():
task = entry.get()
if task:
listbox.insert(tk.END, task)
entry.delete(0, tk.END)
else:
messagebox.showwarning("Warning", "Please enter a task.")
def delete_task():
try:
selected_task = listbox.curselection()[0]
listbox.delete(selected_task)
except IndexError:
messagebox.showwarning("Warning", "Please select a task to delete.")
# Create the main application window
root = tk.Tk()
root.title("To-Do List - The Pycodes")
root.geometry("400x600")
root.configure(bg="black")
tk.Label(root,text="Today's To Do List",font="arial 20 bold",bg="black",fg="red").place(x=70,y=20)
root.resizable(False,False)
# Create an entry widget for adding tasks
entry = tk.Entry(root, width=40,font="arial 12")
entry.pack(pady=100)
# Create a button to add tasks
add_button = tk.Button(root, text="Add Task", command=add_task)
add_button.place(x=175,y=150)
# Create a frame to contain the listbox and scrollbar
frame = tk.Frame(root,bd=3,width=100)
frame.pack()
# Create a listbox to display tasks
listbox = tk.Listbox(frame, width=40,height=15, selectmode=tk.SINGLE,bg="grey",fg="white",font="arial 11")
listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Create a Canvas widget to contain the listbox
canvas = tk.Canvas(frame, bg="grey")
canvas.pack(side=tk.RIGHT, fill=tk.BOTH)
# Create a scrollbar for the listbox
scrollbar = tk.Scrollbar(canvas,orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)
# Configure the listbox to use the scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# Create a button to delete selected tasks
delete_button = tk.Button(root, text="Delete Task", command=delete_task)
delete_button.place(x=170,y=530)
# Start the main event loop
root.mainloop()
Happy Coding!