Python todo list using tkinter


How to create Python Todo list using tkinter

Welcome,

In this blog you will learn how to make Python Todo list using Tkinter

Tkinter is a Python GUI (Graphical User Interface) toolkit, which is a standard Python library used to create GUI applications. It is a cross-platform GUI toolkit that is widely used for building desktop applications in Python.

To build your own GUI applications, you can use Tkinter to create windows, buttons, menus, textboxes, labels, and many other widgets. We’ll go over Tkinter’s fundamental ideas in this blog post, along with how to use it to build a straightforward GUI application.

There is no need to install Tkinter separately because it is already included with Python. However, you might have to manually install it if you’re using an older version of Python.

  1. To install run the following command:
import tkinter

If the import statement executes successfully, then you have Tkinter installed.

2. How to Create a Basic Tkinter Window

import tkinter as tk

window = tk.Tk()
window.title("My First Tkinter Window")
window.geometry("400x300")
window.mainloop()

title() — method sets the title of the window.

geometry() — method sets the size of the window.

mainloop() — method is a method that starts the event loop. [ events, such as mouse clicks and keyboard input, and responds to them.]

3. How to add Widgets to the window ?

import tkinter as tk
window = tk.Tk()
window.title(" Tkinter Window")
window.geometry("400x300")

# Create a label widget
label = tk.Label(window, text="This is a label")
label.pack()

# Create a button widget
button = tk.Button(window, text="This is a button")
button.pack()

window.mainloop()

The pack() method arranges the widgets in the window.

4. How to handle events using function?

When we want to add a specific function to our button click we can add our logic in function add pass it inside button.bind() as mentioned in below example

import tkinter as tk

def button_clicked():
print("Button Clicked!")

window = tk.Tk()
window.title("Tkinter Window")
window.geometry("500x400")

button = tk.Button(window, text="Button Click")
button.pack()
button.bind("<Button-1>", lambda event: button_clicked())

window.mainloop()

Now let’s jump into how to make a Todo List using tkinter

import tkinter

import tkinter.messagebox

import pickle



window = tkinter.Tk()

window.title("To-Do List | Quick | Fast")



def add_task():

task = entry_task.get()

if task != "":

listbox_tasks.insert(tkinter.END, task)

entry_task.delete(0, tkinter.END)

else:

tkinter.messagebox.showwarning(title="Warning!", message="You must enter a task.")



def delete_task():

try:

task_index = listbox_tasks.curselection()[0]

listbox_tasks.delete(task_index)

except:

tkinter.messagebox.showwarning(title="Warning!", message="You must select a task.")



def load_tasks():

try:

tasks = pickle.load(open("tasks.dat", "rb"))

listbox_tasks.delete(0, tkinter.END)

for task in tasks:

listbox_tasks.insert(tkinter.END, task)

except:

tkinter.messagebox.showwarning(title="Warning!", message="Cannot find tasks.dat.")



def save_tasks():

if len(tasks)!=0:

tasks = listbox_tasks.get(0, listbox_tasks.size())

pickle.dump(tasks, open("tasks.dat", "wb"))



# Create GUI

frame_tasks = tkinter.Frame(window)

frame_tasks.pack()



listbox_tasks = tkinter.Listbox(frame_tasks, height=15, width=50,font=("Times New Roman", 12 ))

listbox_tasks.pack(side=tkinter.LEFT)



scrollbar_tasks = tkinter.Scrollbar(frame_tasks)

scrollbar_tasks.pack(side=tkinter.RIGHT, fill=tkinter.Y)



listbox_tasks.config(yscrollcommand=scrollbar_tasks.set)

scrollbar_tasks.config(command=listbox_tasks.yview)



# myFont = Font(family="Times New Roman", size=12)

# text.configure(font=myFont)

# entry_task = tkinter.Entry(window, width=35,font=30)

entry_task = tkinter.Entry(window, width=35,font=("Times New Roman", 12 ))

entry_task.pack()



button_add_task = tkinter.Button(window, text="Add task", width=60, height=3,bg='black',fg='yellow',command=add_task,font=("Kristen ITC",8 ))

button_add_task.pack()



button_delete_task = tkinter.Button(window, text="Delete task", width=60, height=3 ,bg='black',fg='yellow',command=delete_task,font=("Kristen ITC",8 ))

button_delete_task.pack()



button_load_tasks = tkinter.Button(window, text="Load tasks", width=60, height=3,bg='black',fg='yellow',command=load_tasks,font=("Kristen ITC",8 ))

button_load_tasks.pack()



button_save_tasks = tkinter.Button(window, text="Save tasks", width=60, height=3, bg='black',fg='yellow',command=save_tasks,font=("Kristen ITC",8 ))

button_save_tasks.pack()



window.resizable(False, False)

window.geometry('430x560')

window.mainloop()

Here is a breakdown of the code:

  1. Importing necessary modules:
  • tkinter: the main module for creating the GUI
  • tkinter.messagebox: for displaying warning messages.
  • pickle: for saving and loading data to and from a file

2. Creating a Tkinter window using the Tk() method and giving it a title.

3. Defining four functions:

  • add_task(): adds a task to the To-Do List
  • delete_task(): deletes a selected task from the To-Do List
  • load_tasks(): loads previously saved tasks from a file
  • save_tasks(): saves the current tasks to a file

4. Creating the GUI elements:

  • A frame for the To-Do List
  • A Listbox to display the tasks
  • A Scrollbar to scroll through the Listbox
  • An Entry to add new tasks
  • Four Buttons to add, delete, load, and save tasks

5. Configuring the Listbox and Scrollbar to work together.

6. Setting the font and size of the Entry and Buttons.

7. Setting the size and geometry of the window.

8. Running the main event loop using the mainloop() method

Conclusion:

Tkinter is a powerful GUI toolkit that allows you to create desktop applications with ease. In this blog post, we discussed the basic concepts of Tkinter and how to create a simple GUI application using it. With Tkinter, you can create windows, buttons, menus, textboxes, labels, and many other widgets to build your own GUI applications.

Comments

Popular posts from this blog

Pattern python