Create a TO-DO List python application inspired by kanban. The application should allow you to add the activities to be done, the priority level, the deadline (optionally) and it should allow you to drag the activities between the columns. The interface should be minimalist and use darker colors that are comfortable on the eyes. The program should save everything so that it can be resumed when the application is reopened. Give the complete code with all the fuctions fully implemented
Assistant
Creating a Kanban-inspired To-Do List application in Python can be efficiently achieved using the PyQt5 library, which offers robust support for creating feature-rich GUI applications. Below is a comprehensive guide and complete code to help you build this application with the specified features:
Features Implemented:
Add Activities: Users can add tasks with a title, priority level (Low, Medium, High), and an optional deadline.
Drag and Drop: Tasks can be dragged and dropped between columns ("To Do", "In Progress", "Done").
Minimalist Dark Interface: The application uses a dark color scheme that's easy on the eyes.
Persistent Storage: All tasks are saved to a tasks.json file, ensuring that the data persists between sessions.
Prerequisites:
Python 3.x: Ensure you have Python installed on your system.
PyQt5: Install PyQt5 using pip if you haven't already:
bash
pip install PyQt5
Complete Code:
python
import sys
import json
import os
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QLineEdit, QTextEdit, QDialog, QComboBox,
QDateEdit, QMessageBox, QListWidget, QListWidgetItem, QAbstractItemView
)
from PyQt5.QtCore import Qt, QDate, QMimeData
from PyQt5.QtGui import QDrag, QColor, QPalette
# Constants for column statuses
STATUS_TODO = 'To Do'
STATUS_IN_PROGRESS = 'In Progress'
STATUS_DONE = 'Done'
COLUMN_STATUSES = [STATUS_TODO, STATUS_IN_PROGRESS, STATUS_DONE]
# File to save tasks
TASKS_FILE = 'tasks.json'
class Task:
def __init__(self, title, priority, deadline=None, status=STATUS_TODO):
self.title = title
self.priority = priority
self.deadline = deadline # QDate object
self.status = status
def to_dict(self):
return {
'title': self.title,
'priority': self.priority,
'deadline': self.deadline.toString(Qt.ISODate) if self.deadline else None,
'status': self.status
}
@staticmethod
def from_dict(data):
deadline = QDate.fromString(data['deadline'], Qt.ISODate) if data['deadline'] else None
return Task(data['title'], data['priority'], deadline, data['status'])
class TaskWidget(QListWidgetItem):
def __init__(self, task):
super().__init__(task.title)
self.task = task
self.update_display()
def update_display(self):
display_text = f"[{self.task.priority}] {self.task.title}"
if self.task.deadline:
display_text += f" (Due: {self.task.deadline.toString('yyyy-MM-dd')})"
self.setText(display_text)
# Set color based on priority
if self.task.priority == 'High':
self.setBackground(QColor('#f28b82')) # Red-ish
elif self.task.priority == 'Medium':
self.setBackground(QColor('#fbbc04')) # Orange
else:
self.setBackground(QColor('#ccff90')) # Green
class AddTaskDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Add Task")
self.setModal(True)
self.setFixedSize(300, 250)
layout = QVBoxLayout()
# Title
self.title_input = QLineEdit()
self.title_input.setPlaceholderText("Task Title")
layout.addWidget(QLabel("Title:"))
layout.addWidget(self.title_input)
# Priority
self.priority_combo = QComboBox()
self.priority_combo.addItems(['Low', 'Medium', 'High'])
layout.addWidget(QLabel("Priority:"))
layout.addWidget(self.priority_combo)
# Deadline
self.deadline_edit = QDateEdit()
self.deadline_edit.setCalendarPopup(True)
self.deadline_edit.setDate(QDate.currentDate())
layout.addWidget(QLabel("Deadline (optional):"))
layout.addWidget(self.deadline_edit)
# Deadline checkbox
self.deadline_checkbox = QPushButton("Set Deadline")
self.deadline_checkbox.setCheckable(True)
self.deadline_checkbox.clicked.connect(self.toggle_deadline)
layout.addWidget(self.deadline_checkbox)
self.deadline_edit.setEnabled(False)
# Buttons
button_layout = QHBoxLayout()
self.add_button = QPushButton("Add")
self.add_button.clicked.connect(self.accept)
self.cancel_button = QPushButton("Cancel")
self.cancel_button.clicked.connect(self.reject)
button_layout.addWidget(self.add_button)
button_layout.addWidget(self.cancel_button)
layout.addLayout(button_layout)
self.setLayout(layout)
def toggle_deadline(self):
if self.deadline_checkbox.isChecked():
self.deadline_edit.setEnabled(True)
else:
self.deadline_edit.setEnabled(False)
def get_task_data(self):
title = self.title_input.text().strip()
priority = self.priority_combo.currentText()
deadline = self.deadline_edit.date() if self.deadline_edit.isEnabled() else None
return title, priority, deadline
class ColumnWidget(QListWidget):
def __init__(self, status):
super().__init__()
self.status = status
self.setAcceptDrops(True)
self.setDragEnabled(True)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.setStyleSheet("""
QListWidget {
background-color: #2c2c2c;
color: #ffffff;
border: none;
}
QListWidget::item:selected {
background-color: #555555;
}
""")
def startDrag(self, supportedActions):
item = self.currentItem()
if item:
drag = QDrag(self)
mimeData = QMimeData()
mimeData.setText(json.dumps(item.task.to_dict()))
drag.setMimeData(mimeData)
drag.exec_(Qt.MoveAction)
def dragEnterEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
def dragMoveEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
def dropEvent(self, event):
if event.mimeData().hasText():
data = json.loads(event.mimeData().text())
task = Task.from_dict(data)
task.status = self.status
self.add_task(task)
self.parent().remove_task(task)
event.acceptProposedAction()
def add_task(self, task):
item = TaskWidget(task)
self.addItem(item)
class KanbanBoard(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Kanban To-Do List")
self.setGeometry(100, 100, 900, 600)
self.setStyleSheet("""
QMainWindow {
background-color: #1e1e1e;
}
QLabel {
color: #ffffff;
font-size: 16px;
}
QPushButton {
background-color: #3c3c3c;
color: #ffffff;
border: none;
padding: 10px;
}
QPushButton:hover {
background-color: #555555;
}
""")
self.tasks = []
self.load_tasks()
main_widget = QWidget()
main_layout = QVBoxLayout()
# Header
header_layout = QHBoxLayout()
header_layout.addWidget(QLabel("Kanban To-Do List"))
header_layout.addStretch()
add_button = QPushButton("Add Task")
add_button.clicked.connect(self.open_add_task_dialog)
header_layout.addWidget(add_button)
main_layout.addLayout(header_layout)
# Columns
columns_layout = QHBoxLayout()
self.columns = {}
for status in COLUMN_STATUSES:
column_layout = QVBoxLayout()
label = QLabel(status)
label.setAlignment(Qt.AlignCenter)
column_layout.addWidget(label)
column = ColumnWidget(status)
self.columns[status] = column
column_layout.addWidget(column)
columns_layout.addLayout(column_layout)
main_layout.addLayout(columns_layout)
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
# Populate columns with existing tasks
self.populate_columns()
def open_add_task_dialog(self):
dialog = AddTaskDialog()
if dialog.exec_() == QDialog.Accepted:
title, priority, deadline = dialog.get_task_data()
if not title:
QMessageBox.warning(self, "Input Error", "Task title cannot be empty.")
return
task = Task(title, priority, deadline)
self.tasks.append(task)
self.columns[task.status].add_task(task)
self.save_tasks()
def populate_columns(self):
for task in self.tasks:
self.columns[task.status].add_task(task)
def remove_task(self, moved_task):
self.tasks = [task for task in self.tasks if not (
task.title == moved_task.title and
task.priority == moved_task.priority and
task.deadline == moved_task.deadline and
task.status == moved_task.status
)]
def load_tasks(self):
if os.path.exists(TASKS_FILE):
with open(TASKS_FILE, 'r') as f:
data = json.load(f)
for task_data in data:
task = Task.from_dict(task_data)
self.tasks.append(task)
def save_tasks(self):
with open(TASKS_FILE, 'w') as f:
json.dump([task.to_dict() for task in self.tasks], f, indent=4)
def closeEvent(self, event):
# Gather all tasks from columns
all_tasks = []
for status, column in self.columns.items():
for index in range(column.count()):
item = column.item(index)
task = item.task
all_tasks.append(task)
self.tasks = all_tasks
self.save_tasks()
event.accept()
def main():
app = QApplication(sys.argv)
# Set dark palette
palette = QPalette()
palette.setColor(QPalette.Window, QColor('#1e1e1e'))
palette.setColor(QPalette.WindowText, Qt.white)
palette.setColor(QPalette.Base, QColor('#2c2c2c'))
palette.setColor(QPalette.AlternateBase, QColor('#3c3c3c'))
palette.setColor(QPalette.ToolTipBase, Qt.white)
palette.setColor(QPalette.ToolTipText, Qt.white)
palette.setColor(QPalette.Text, Qt.white)
palette.setColor(QPalette.Button, QColor('#3c3c3c'))
palette.setColor(QPalette.ButtonText, Qt.white)
palette.setColor(QPalette.BrightText, Qt.red)
app.setPalette(palette)
window = KanbanBoard()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Explanation of the Code:
Task Class:
Represents a task with a title, priority, optional deadline, and status.
Provides methods to convert to and from dictionaries for JSON serialization.
TaskWidget Class:
Subclass of QListWidgetItem that visually represents a task.
Updates its display based on the task's priority and deadline.
AddTaskDialog Class:
A dialog window that allows users to input task details.
Includes fields for title, priority, and an optional deadline.
ColumnWidget Class:
Subclass of QListWidget representing each Kanban column.
Implements drag-and-drop functionality to move tasks between columns.
KanbanBoard Class:
The main window that sets up the Kanban board with three columns: "To Do", "In Progress", and "Done".
Handles loading and saving tasks to a JSON file.
Provides a button to add new tasks via the AddTaskDialog.
Main Function:
Sets up the application, applies a dark theme, and launches the Kanban board.
Running the Application:
Save the above code to a file named kanban.py (or any name you prefer). Open your terminal or command prompt, navigate to the directory containing the file, and run:
bash
python kanban.py
This will launch the Kanban To-Do List application with the specified features. You can add tasks, set their priority and deadlines, drag them between columns, and all your tasks will be saved automatically when you close the application.
Customization:
Feel free to customize the application further by adding features such as:
Editing Tasks: Allow users to edit existing tasks.
Deleting Tasks: Provide an option to remove tasks.
Filtering and Sorting: Enable filtering tasks based on priority or deadline.
Responsive Design: Make the UI responsive to different window sizes.
This implementation provides a solid foundation for a Kanban-style To-Do List application with a minimalist dark interface and essential functionalities. You can build upon this to enhance its capabilities based on your requirements.