To Do List in Python

In this tutorial, we’re going to learn how to create a To-Do list in Python using Tkinter. But first, we need to understand what Tkinter is. Now, Python allows multiple options to implement GUI (Graphical User Interface). Tkinter is one of the most popular methods to create simple GUI based applications, using the Tk GUI toolkit.

Creating a GUI using Tkinter is actually very easy. Follow these steps:

  1. We need to first import the Tkinter module in our source code.
  2. Next, we can create a main window of the GUI and give it a suitable name.
  3. Now, we can create any number of features in our GUI window, like message box, buttons, etc.
  4. We can also specify an event trigger on each of the GUI features.

 

Let’s first check how to import the Tkinter GUI toolkit:-

from tkinter import *

Creating a To-Do List in Python

Now, we’ll learn how to implement the same to create a To-Do list in Python:-

import tkinter
from tkinter import *
from tkinter import messagebox 

l=[] 
c=1

def entryError() : 
   
  if insertField.get() == "" : 
    
    
    messagebox.showerror("Error in input. Please input again") 
    
    return 0
  
  return 1

def insertTask(): 

  global c 
  
  value = entryError() 

  if (value == 0): 
    return
  var=insertField.get()+"\n"

  l.append(var) 
  TextArea.insert('end -1 chars', str(c) + "---> " + var) 
  c=c+1
  del_tf()

def del_nf() : 
  
  nf.delete(0.0, END) 


def del_tf() : 

  insertField.delete(0, END) 
  
def delete() : 
  
  global c 
  
  if (len(l)==0): 
    messagebox.showerror("There are no tasks") 
    return
  number = nf.get(1.0, END) 

  if (number=="\n"): 
    messagebox.showerror("input error") 
    return
  else : 
    task_no = int(number) 

  del_nf() 
  
  l.pop(task_no - 1) 
  c=c-1
  
  TextArea.delete(1.0, END) 

  for i in range(len(l)): 
    TextArea.insert('end -1 chars',str(i + 1) + "---> " + l[i]) 
  

if (__name__ == "__main__"): 

  window = Tk() 
  window.configure(background = "green") 
  window.title("To-Do List") 
  window.geometry("300x300") 

  enterTask = Label(window, text = "Please enter your task", bg = "green")
  insertField = Entry(window)
  
  Submit = Button(window, text = "Submit", fg = "Black", bg = "light green", command = insertTask) 

  TextArea = Text(window, height = 4, width = 25, font = "arial 13")

  taskNumber = Label(window, text = "Specify the task number that you want to remove, below", bg = "cyan")
            
  nf = Text(window, height = 1, width = 2, font = "arial 13")
  
  delete = Button(window, text = "Delete", fg = "Black", bg = "orange", command = delete) 
  
  Exit = Button(window, text = "Do you want to close?", fg = "Black", bg = "Red", command = exit) 

  enterTask.grid(row = 0, column = 2) 
  insertField.grid(row = 1, column = 2, ipadx = 50) 
  Submit.grid(row = 2, column = 2) 
  TextArea.grid(row = 3, column = 2, padx = 10, sticky = W) 
  taskNumber.grid(row = 4, column = 2, pady = 5) 
  nf.grid(row = 5, column = 2) 
  delete.grid(row = 6, column = 2, pady = 5) 
  Exit.grid(row = 7, column = 2) 
  
  window.mainloop() 

After executing the above code, we’ll find the To-Do list application has been created and launched. Now, we’re be able to use our own Python based To-Do list.

 

You can also check out this, for better understanding of Tkinter GUI toolkit:

Python GUI-tkinter

Leave a Reply

Your email address will not be published. Required fields are marked *