Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GUI in todo list with task priority feature #621

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions projects/ToDoList/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
# ToDo List in Python 📒
# To-Do List Application

This is a simple command-line ToDo List application written in Python.
This is a simple to-do list application with a graphical user interface (GUI) created in Python using the tkinter library. It allows users to add, remove, and view tasks using a user-friendly interface.

## Features

- **Add Tasks:** You can add tasks to your ToDo list. Just type in your task when prompted.
- Add tasks to the to-do list.
- Remove tasks from the to-do list by selecting and deleting them.
- View all tasks in the to-do list.
- User-friendly graphical interface.
- Prioritize tasks with different colors (High priority in red, Medium in orange, Low in green).
- Set due dates for tasks and select them from a calendar view.
- Quit the application.

- **Remove Tasks:** You can remove tasks from your ToDo list. You will need to provide the task number (the number displayed next to each task when displaying tasks).

- **Display Tasks:** You can display all tasks in your ToDo list in the order they were added.
## Usage

- **Quit:** You can quit the application at any time.
1. Before running the application, make sure to install the `tkcalendar` library. You can do this by running the following command:

## Usage
```bash
pip install tkcalendar
```

To run the program, you will need Python installed on your computer. Once you have Python, you can run the program from the command line by navigating to the directory containing the Python file and running the following command:
2. Run the application by executing the Python script `todo.py`.

```bash
python to_do_list.py
python3 to_do_list.py
```bash
python to_do_list.py
```
223 changes: 142 additions & 81 deletions projects/ToDoList/to_do_list.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,146 @@
from pickle import dump, load


def add_task(task):
todo_list.append(task)
print("Task added!")


def remove_task(task_num):
if 0 <= task_num < len(todo_list):
del todo_list[task_num]
print("Task removed!")
else:
print("Invalid task number!")


def display_tasks():
if not todo_list: # If list is empty
print("No tasks to display.")
else:
for index, task in enumerate(todo_list, start=1):
print(f"{index}. {task}")


def get_choice():
while True:
try:
choice = int(
input(
"Type a number: 1. Adding a task, 2. Removing a task, 3. Displaying tasks, 4. Quit: "
)
)
if 1 <= choice <= 4:
return choice
else:
print("Invalid choice. Try again.")
except ValueError:
print("Please enter a number between 1 and 4.")


if __name__ == "__main__":
# Loading the pickle file into python as a list
import tkinter as tk
from tkinter import simpledialog
from datetime import datetime
from tkcalendar import Calendar
import pickle

# Define an empty list to store tasks
tasks = []

# Priority colors
priority_colors = {
"High": "red",
"Medium": "orange",
"Low": "green",
}

# Load tasks from a file (if it exists)
def load_tasks():
try:
with open("todo.pickle", "rb+") as file_in:
todo_list = load(file_in)
with open("tasks.pkl", "rb") as file:
return pickle.load(file)
except FileNotFoundError:
todo_list = []

print("Welcome to ToDo List!")
return []

while True:
user_choice = get_choice()
tasks = load_tasks()

# Adding a task
if user_choice == 1:
new_task = input("Type a new task: ")
add_task(new_task)

# Removing a task
elif user_choice == 2:
if not todo_list: # If list is empty
print("No tasks to remove.")
else:
task_num = int(input("Enter the task number to delete: ")) - 1
remove_task(task_num)

# Displaying tasks
elif user_choice == 3:
display_tasks()

# Quit
elif user_choice == 4:
# Dumping the list into a pickle file
with open("todo.pickle", "wb") as file_out:
dump(todo_list, file_out)
print("Goodbye!")
break


#####################################

# CODE CONTRIBUTED BY: Ota Hina
# Dynamic funcionality added by : komsenapati

#####################################
# Create a function to load and display tasks
def display_tasks():
for task_info in tasks:
task = task_info["task"]
priority = task_info["priority"]
due_date = task_info["due_date"]
task_text = f"{len(task_list.get(0, 'end')) + 1}. {task} (Priority: {priority}, Due Date: {due_date})"
task_list.insert("end", task_text)
task_list.itemconfig("end", {'fg': priority_colors.get(priority, 'black')})

# Adding Task
def add_task():
task = task_entry.get()
priority = task_priority.get()
due_date = task_due_date.get()

if task:
task_info = {
"task": task,
"priority": priority,
"due_date": due_date
}
tasks.append(task_info)
task_entry.delete(0, "end") # Clear the entry field
update_task_list()
status_label.config(text="Task added: " + task, fg="green")
save_tasks_to_file()

# Removing Task
def remove_selected_task():
selected_task = task_list.curselection()
if selected_task:
index = selected_task[0]
task_info = tasks[index]
tasks.pop(index)
update_task_list()
status_label.config(text="Task removed: " + task_info['task'], fg="red")
save_tasks_to_file()
else:
status_label.config(text="No task selected.", fg="red")

# Save tasks to a file
def save_tasks_to_file():
with open("tasks.pkl", "wb") as file:
pickle.dump(tasks, file)

# Show the calendar for selecting due date
def show_calendar():
date_str = task_due_date.get()
if date_str:
selected_date = datetime.strptime(date_str, "%Y-%m-%d")
else:
selected_date = datetime.now()

new_date = simpledialog.askstring("Select Due Date", "Enter a date (YYYY-MM-DD):", parent=root, initialvalue=selected_date.strftime("%Y-%m-%d"))

if new_date:
task_due_date.delete(0, "end")
task_due_date.insert(0, new_date)

# Update the task list in the UI with priority-based colors
def update_task_list():
task_list.delete(0, "end")
for index, task_info in enumerate(tasks, start=1):
task = task_info["task"]
priority = task_info["priority"]
due_date = task_info["due_date"]
task_text = f"{index}. {task} (Priority: {priority}, Due Date: {due_date})"
task_list.insert("end", task_text)
task_list.itemconfig(index, {'fg': priority_colors.get(priority, 'black')})

# Create the main window
root = tk.Tk()
root.title("Interactive To-Do List with Priority and Due Date")

# Create an entry field for tasks
task_entry = tk.Entry(root, width=30)
task_entry.pack(pady=10)

# Create a priority dropdown
priority_options = ["High", "Medium", "Low"]
task_priority = tk.StringVar()
task_priority.set(priority_options[0]) # Set default value
priority_label = tk.Label(root, text="Priority:")
priority_label.pack()
priority_menu = tk.OptionMenu(root, task_priority, *priority_options)
priority_menu.pack()

# Create a due date entry with a calendar button
due_date_label = tk.Label(root, text="Due Date (YYYY-MM-DD):")
due_date_label.pack()
due_date_frame = tk.Frame(root)
due_date_frame.pack()
task_due_date = tk.Entry(due_date_frame, width=10)
task_due_date.pack(side="left")
calendar_button = tk.Button(due_date_frame, text="Calendar", command=show_calendar)
calendar_button.pack(side="left")

# Create buttons for Add, Remove, and Quit
add_button = tk.Button(root, text="Add Task", command=add_task)
remove_button = tk.Button(root, text="Remove Task", command=remove_selected_task)
quit_button = tk.Button(root, text="Quit", command=root.quit)

add_button.pack()
remove_button.pack()
quit_button.pack()

# Create a listbox to display tasks
task_list = tk.Listbox(root, selectmode=tk.SINGLE, width=60)
task_list.pack()

# Create a status label for feedback
status_label = tk.Label(root, text="", fg="black")
status_label.pack()

# Load and display tasks
display_tasks()

# Run the application
root.mainloop()