Welcome folks today in this blog post we will be building a todo list widget dynamically using entry text input
in tkinter
gui desktop app in python. All the full source code of the application is given below.
Get Started
In order to get started you need to install the below library using the pip
command as shown below
pip install tkinter
After installing this library you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import tkinter as tk class Todo(tk.Tk): def __init__(self, tasks=None): super().__init__() if not tasks: self.tasks = [] else: self.tasks = tasks self.title("To-Do App v1") self.geometry("300x400") todo1 = tk.Label(self, text="--- Add Items Here ---", bg="lightgrey", fg="black", pady=10) self.tasks.append(todo1) for task in self.tasks: task.pack(side=tk.TOP, fill=tk.X) self.task_create = tk.Text(self, height=3, bg="white", fg="black") self.task_create.pack(side=tk.BOTTOM, fill=tk.X) self.task_create.focus_set() self.bind("<Return>", self.add_task) self.colour_schemes = [{"bg": "lightgrey", "fg": "black"}, {"bg": "grey", "fg": "white"}] def add_task(self, event=None): task_text = self.task_create.get(1.0,tk.END).strip() if len(task_text) > 0: new_task = tk.Label(self, text=task_text, pady=10) _, task_style_choice = divmod(len(self.tasks), 2) my_scheme_choice = self.colour_schemes[task_style_choice] new_task.configure(bg=my_scheme_choice["bg"]) new_task.configure(fg=my_scheme_choice["fg"]) new_task.pack(side=tk.TOP, fill=tk.X) self.tasks.append(new_task) self.task_create.delete(1.0, tk.END) if __name__ == "__main__": todo = Todo() todo.mainloop() |
If you execute the above python script
by typing the below command
python app.py