To-do-app/to-do-app.py

171 lines
6.6 KiB
Python
Raw Permalink Normal View History

2024-06-20 12:16:55 +02:00
import sqlite3
import tkinter as tk
from tkinter import messagebox
def create_db():
conn = sqlite3.connect('tasks.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Tasks (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
completed BOOLEAN NOT NULL
)
''')
conn.commit()
conn.close()
create_db()
class Task:
def __init__(self, taskname, isdone=False):
self.taskname = taskname
self.isdone = isdone
def get_tasks():
connection = sqlite3.connect('tasks.db')
cursor = connection.cursor()
cursor.execute('SELECT * FROM Tasks')
tasks = cursor.fetchall()
connection.close()
tasks_list = [{'id': row[0], 'title': row[1], 'completed': row[2]} for row in tasks]
return tasks_list
def add_task(taskname):
connection = sqlite3.connect('tasks.db')
new_task = Task(taskname=taskname, isdone=False)
cursor = connection.cursor()
cursor.execute('INSERT INTO Tasks (title, completed) VALUES (?, ?)', (new_task.taskname, new_task.isdone))
connection.commit()
connection.close()
def delete_task(task_id):
connection = sqlite3.connect('tasks.db')
cursor = connection.cursor()
cursor.execute('DELETE FROM Tasks WHERE id = ?', (task_id,))
connection.commit()
connection.close()
def delete_all():
connection = sqlite3.connect('tasks.db')
cursor = connection.cursor()
cursor.execute('DELETE FROM Tasks')
connection.commit()
connection.close()
def mark_as_done(task_id):
connection = sqlite3.connect('tasks.db')
cursor = connection.cursor()
cursor.execute('UPDATE Tasks SET completed = ? WHERE id = ?', (True, task_id))
connection.commit()
connection.close()
class TaskApp:
def __init__(self, root):
self.root = root
root.configure(bg='lightblue')
self.root.title("To-do App")
buttonfont=('Arial', 10)
tk.Label(root, text='Your Tasks', bg='lightblue', font=('Arial', 20)).grid(row=0, column=0, columnspan=3, padx=5, pady=10)
self.task_listbox = tk.Listbox(root, width=45, height=15, bg='light yellow', selectmode=tk.SINGLE)
self.task_listbox.grid(row=1, column=0, columnspan=3, padx=0, pady=5)
tk.Label(root, text="Enter new task:", bg='lightblue', font=buttonfont).grid(row=2, column=0, padx=5, pady=5)
self.entry_task = tk.Entry(root, width=25, bg='light yellow')
self.entry_task.grid(row=2, column=1, padx=0, pady=5)
self.add_button = tk.Button(root, text="Add Task", bg='light yellow', font=buttonfont, width=10, command=self.add_task)
self.add_button.grid(row=2, column=2, columnspan=1, padx=5, pady=5)
self.delete_button = tk.Button(root, text="Delete Task", bg='light yellow', font=buttonfont,width=10, command=self.delete_task)
self.delete_button.grid(row=4, column=0, columnspan=2, padx=2, pady=5, sticky=tk.E)
self.complete_button = tk.Button(root, text="Mark as done", bg='light yellow', font=buttonfont,width=10, command=self.mark_as_done)
self.complete_button.grid(row=4, column=0, columnspan=2, pady=5, padx=10, sticky=tk.W)
self.delete_all_button = tk.Button(root, text="Delete All", bg='light yellow', font=buttonfont,width=10, command=self.delete_all)
self.delete_all_button.grid(row=4, column=2, columnspan=1, pady=5, padx=5)
self.search_entry = tk.Entry(root, width=25, bg='light yellow')
self.search_entry.grid(row=3, column=1, padx=0, pady=5)
tk.Label(root, text="Enter task to search:", bg='lightblue', font=buttonfont).grid(row=3, column=0, padx=5, pady=5)
self.search_button = tk.Button(root, text="Search Task", bg='light yellow', font=buttonfont,width=10, command=self.search_task)
self.search_button.grid(row=3, column=2, columnspan=1, padx=5, pady=5)
self.sort_button = tk.Button(root, text="Sort Tasks", bg='light yellow', font=buttonfont,width=10, command=self.sort_tasks)
self.sort_button.grid(row=4, column=0, columnspan=2, pady=5, padx=(14,5))
self.load_tasks()
def load_tasks(self):
self.task_listbox.delete(0, tk.END)
tasks = Task.get_tasks()
for task in tasks:
task_str = f"{task['title']} - {'Done' if task['completed'] else 'Not Done'}"
self.task_listbox.insert(tk.END, task_str)
def add_task(self):
taskname = self.entry_task.get()
if taskname:
Task.add_task(taskname)
self.load_tasks()
self.entry_task.delete(0, tk.END)
else:
messagebox.showwarning("Warning", "Task name cannot be empty")
def delete_task(self):
try:
selected_task_index = self.task_listbox.curselection()[0]
selected_task_name = self.task_listbox.get(selected_task_index).split(' - ')[0]
task_id = [task['id'] for task in Task.get_tasks() if task['title'] == selected_task_name][0]
Task.delete_task(task_id)
self.load_tasks()
except IndexError:
messagebox.showwarning("Warning", "Please select a task to delete")
def delete_all(self):
Task.delete_all()
self.load_tasks()
def mark_as_done(self):
try:
selected_task_index = self.task_listbox.curselection()[0]
selected_task_name = self.task_listbox.get(selected_task_index).split(' - ')[0]
task_id = [task['id'] for task in Task.get_tasks() if task['title'] == selected_task_name][0]
Task.mark_as_done(task_id)
self.load_tasks()
except IndexError:
messagebox.showwarning("Warning", "Please select a task to complete")
def search_task(self):
search_text = self.search_entry.get()
if search_text:
tasks = Task.get_tasks()
for index, task in enumerate(tasks):
if search_text.lower() in task['title'].lower():
self.task_listbox.select_set(index)
return
messagebox.showinfo("Info", f"Task not found")
else:
messagebox.showwarning("Warning", "Search text cannot be empty")
def sort_tasks(self):
tasks = Task.get_tasks()
sorted_tasks = sorted(tasks, key=lambda x: (not x['completed']))
self.task_listbox.delete(0, tk.END)
for task in sorted_tasks:
task_str = f"{task['title']} - {'Done' if task['completed'] else 'Not Done'}"
self.task_listbox.insert(tk.END, task_str)
if __name__ == "__main__":
root = tk.Tk()
app = TaskApp(root)
root.mainloop()