shuaihaowang / TaskManager

A2: Git Repository
0 stars 0 forks source link

A2: Git Repository #1

Open shuaihaowang opened 1 week ago

shuaihaowang commented 1 week ago

import tkinter as tk from tkinter import ttk

TaskManagerUI class responsible for building the UI

class TaskManagerUI: def init(self, root): self.root = root self.root.title("Task Manager") self.root.geometry("800x600")

    # Initialize the main frames
    self.setup_frames()

    # Initialize task list as an empty list for now (to be integrated with backend logic later)
    self.tasks = []

    # Setup UI components
    self.setup_task_creation_form()
    self.setup_task_list_display()
    self.setup_filter_sorting_section()

def setup_frames(self):
    # Top frame for task creation form
    self.task_form_frame = tk.Frame(self.root)
    self.task_form_frame.pack(fill=tk.X, padx=10, pady=10)

    # Middle frame for task list display
    self.task_list_frame = tk.Frame(self.root)
    self.task_list_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

    # Bottom frame for filtering and sorting
    self.filter_sort_frame = tk.Frame(self.root)
    self.filter_sort_frame.pack(fill=tk.X, padx=10, pady=10)

def setup_task_creation_form(self):
    # Labels and input fields for task creation
    tk.Label(self.task_form_frame, text="Title:").grid(row=0, column=0, padx=5, pady=5)
    self.title_entry = tk.Entry(self.task_form_frame)
    self.title_entry.grid(row=0, column=1, padx=5, pady=5)

    tk.Label(self.task_form_frame, text="Description:").grid(row=0, column=2, padx=5, pady=5)
    self.description_entry = tk.Entry(self.task_form_frame)
    self.description_entry.grid(row=0, column=3, padx=5, pady=5)

    tk.Label(self.task_form_frame, text="Deadline:").grid(row=1, column=0, padx=5, pady=5)
    self.deadline_entry = tk.Entry(self.task_form_frame)
    self.deadline_entry.grid(row=1, column=1, padx=5, pady=5)

    tk.Label(self.task_form_frame, text="Priority:").grid(row=1, column=2, padx=5, pady=5)
    self.priority_var = tk.StringVar()
    self.priority_menu = ttk.Combobox(self.task_form_frame, textvariable=self.priority_var)
    self.priority_menu['values'] = ['Low', 'Medium', 'High']
    self.priority_menu.grid(row=1, column=3, padx=5, pady=5)

    tk.Label(self.task_form_frame, text="Project:").grid(row=2, column=0, padx=5, pady=5)
    self.project_entry = tk.Entry(self.task_form_frame)
    self.project_entry.grid(row=2, column=1, padx=5, pady=5)

    # Buttons for adding and editing tasks
    self.add_task_button = tk.Button(self.task_form_frame, text="Add Task", command=self.add_task)
    self.add_task_button.grid(row=2, column=2, padx=5, pady=5)

    self.edit_task_button = tk.Button(self.task_form_frame, text="Edit Task", state=tk.DISABLED)
    self.edit_task_button.grid(row=2, column=3, padx=5, pady=5)

def setup_task_list_display(self):
    # Setup treeview for task list display
    columns = ('Title', 'Project', 'Deadline', 'Priority')
    self.task_tree = ttk.Treeview(self.task_list_frame, columns=columns, show='headings')

    # Define column headings
    for col in columns:
        self.task_tree.heading(col, text=col)

    # Set column sizes
    self.task_tree.column('Title', width=150)
    self.task_tree.column('Project', width=100)
    self.task_tree.column('Deadline', width=100)
    self.task_tree.column('Priority', width=100)

    # Pack the treeview widget
    self.task_tree.pack(fill=tk.BOTH, expand=True)

    # Add scrollbar for large task lists
    self.task_scrollbar = ttk.Scrollbar(self.task_list_frame, orient="vertical", command=self.task_tree.yview)
    self.task_tree.configure(yscroll=self.task_scrollbar.set)
    self.task_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

def setup_filter_sorting_section(self):
    # Filtering and sorting options
    tk.Label(self.filter_sort_frame, text="Filter by Priority:").grid(row=0, column=0, padx=5, pady=5)
    self.filter_priority_var = tk.StringVar()
    self.filter_priority_menu = ttk.Combobox(self.filter_sort_frame, textvariable=self.filter_priority_var)
    self.filter_priority_menu['values'] = ['All', 'Low', 'Medium', 'High']
    self.filter_priority_menu.grid(row=0, column=1, padx=5, pady=5)

    self.sort_deadline_button = tk.Button(self.filter_sort_frame, text="Sort by Deadline")
    self.sort_deadline_button.grid(row=0, column=2, padx=5, pady=5)

def add_task(self):
    # Function to add tasks (initially just prints values, to be connected with backend later)
    task_title = self.title_entry.get()
    task_description = self.description_entry.get()
    task_deadline = self.deadline_entry.get()
    task_priority = self.priority_var.get()
    task_project = self.project_entry.get()

    if task_title and task_priority:
        # Add task to the task list (just appending to tasks list for now)
        task = (task_title, task_project, task_deadline, task_priority)
        self.tasks.append(task)

        # Insert into the Treeview
        self.task_tree.insert('', 'end', values=task)

        # Clear input fields
        self.clear_task_form()
    else:
        print("Task title and priority are required")

def load_task_for_editing(self, event):
    # Load selected task into the form for editing
    selected_item = self.task_tree.selection()[0]  # Get the selected item
    selected_task = self.task_tree.item(selected_item, 'values')

    # Find the index of the selected task in the tasks list
    self.selected_task_index = self.task_tree.index(selected_item)

    # Load task details into the form
    self.title_entry.delete(0, tk.END)
    self.title_entry.insert(0, selected_task[0])

    self.project_entry.delete(0, tk.END)
    self.project_entry.insert(0, selected_task[1])

    self.deadline_entry.delete(0, tk.END)
    self.deadline_entry.insert(0, selected_task[2])

    self.priority_var.set(selected_task[3])

    # Enable the "Edit Task" button
    self.edit_task_button.config(state=tk.NORMAL)

def edit_task(self):
    if self.selected_task_index is not None:
        # Get updated task details from the form
        updated_title = self.title_entry.get()
        updated_description = self.description_entry.get()
        updated_deadline = self.deadline_entry.get()
        updated_priority = self.priority_var.get()
        updated_project = self.project_entry.get()

        # Update task in the list and Treeview
        updated_task = (updated_title, updated_project, updated_deadline, updated_priority)
        self.tasks[self.selected_task_index] = updated_task
        selected_item = self.task_tree.selection()[0]
        self.task_tree.item(selected_item, values=updated_task)

        # Clear input fields
        self.clear_task_form()
        self.selected_task_index = None
        self.edit_task_button.config(state=tk.DISABLED)

def clear_task_form(self):
    self.title_entry.delete(0, tk.END)
    self.description_entry.delete(0, tk.END)
    self.deadline_entry.delete(0, tk.END)
    self.priority_var.set('')
    self.project_entry.delete(0, tk.END)