-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_python_do.py
More file actions
96 lines (73 loc) · 3.16 KB
/
generate_python_do.py
File metadata and controls
96 lines (73 loc) · 3.16 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import datetime
class ToDoApp:
"""
A simple to-do list application built using Tkinter.
"""
def __init__(self, master):
"""Initializes the to-do list application."""
self.master = master
master.title("To-Do List")
self.tasks = [] # List to store to-do items
# UI elements
self.task_entry = ttk.Entry(master, width=30)
self.task_entry.pack(pady=5)
self.due_date_entry = ttk.Entry(master, width=15)
self.due_date_entry.pack(pady=2)
self.due_date_label = ttk.Label(master, text="Due Date (YYYY-MM-DD):")
self.due_date_label.pack()
self.add_button = ttk.Button(master, text="Add Task", command=self.add_task)
self.add_button.pack(pady=5)
self.task_listbox = tk.Listbox(master, selectmode="SINGLE", width=40)
self.task_listbox.pack(pady=10)
self.remove_button = ttk.Button(master, text="Remove Task", command=self.remove_task)
self.remove_button.pack(pady=5)
self.complete_button = ttk.Button(master, text="Mark as Complete", command=self.mark_complete)
self.complete_button.pack(pady=5)
self.update_listbox()
def add_task(self):
"""Adds a new task to the to-do list."""
task = self.task_entry.get().strip()
due_date = self.due_date_entry.get().strip()
if not task:
messagebox.showerror("Error", "Please enter a task.")
return
try:
if due_date:
datetime.datetime.strptime(due_date, '%Y-%m-%d') #Validate date format
except ValueError:
messagebox.showerror("Error", "Invalid date format. Use YYYY-MM-DD.")
return
new_task = {"task": task, "completed": False, "due_date": due_date}
self.tasks.append(new_task)
self.task_entry.delete(0, tk.END)
self.due_date_entry.delete(0, tk.END)
self.update_listbox()
def remove_task(self):
"""Removes a selected task from the to-do list."""
try:
selection = self.task_listbox.curselection()[0]
del self.tasks[selection]
self.update_listbox()
except IndexError:
messagebox.showwarning("Warning", "Please select a task to remove.")
def mark_complete(self):
"""Marks a selected task as complete."""
try:
selection = self.task_listbox.curselection()[0]
self.tasks[selection]["completed"] = not self.tasks[selection]["completed"]
self.update_listbox()
except IndexError:
messagebox.showwarning("Warning", "Please select a task to mark as complete.")
def update_listbox(self):
"""Updates the listbox with the current to-do items."""
self.task_listbox.delete(0, tk.END)
for i, task in enumerate(self.tasks):
status = "[x]" if task["completed"] else "[ ]"
due_date_str = f" (Due: {task['due_date']})" if task['due_date'] else ""
self.task_listbox.insert(i, f"{status} {task['task']}{due_date_str}")
root = tk.Tk()
app = ToDoApp(root)
root.mainloop()