-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtodo-with-delete.py
103 lines (85 loc) · 2.97 KB
/
todo-with-delete.py
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
96
97
98
99
100
101
102
103
import pglet
from pglet import Stack, Textbox, Button, Checkbox
class Task:
def __init__(self, app, name):
self.app = app
self.display_task = Checkbox(value=False, label=name)
self.edit_name = Textbox(width="100%")
self.display_view = Stack(
horizontal=True,
horizontal_align="space-between",
vertical_align="center",
controls=[
self.display_task,
Stack(
horizontal=True,
gap="0",
controls=[
Button(
icon="Edit", title="Edit todo", on_click=self.edit_clicked
),
Button(
icon="Delete",
title="Delete todo",
on_click=self.delete_clicked,
),
],
),
],
)
self.edit_view = Stack(
visible=False,
horizontal=True,
horizontal_align="space-between",
vertical_align="center",
controls=[self.edit_name, Button(text="Save", on_click=self.save_clicked)],
)
self.view = Stack(controls=[self.display_view, self.edit_view])
def edit_clicked(self, e):
self.edit_name.value = self.display_task.label
self.display_view.visible = False
self.edit_view.visible = True
self.view.update()
def save_clicked(self, e):
self.display_task.label = self.edit_name.value
self.display_view.visible = True
self.edit_view.visible = False
self.view.update()
def delete_clicked(self, e):
self.app.delete_task(self)
class TodoApp:
def __init__(self):
self.tasks = []
self.new_task = Textbox(placeholder="Whats needs to be done?", width="100%")
self.tasks_view = Stack()
# application's root control (i.e. "view") containing all other controls
self.view = Stack(
width="70%",
controls=[
Stack(
horizontal=True,
on_submit=self.add_clicked,
controls=[self.new_task, Button("Add", on_click=self.add_clicked)],
),
self.tasks_view,
],
)
def add_clicked(self, e):
task = Task(self, self.new_task.value)
self.tasks.append(task)
self.tasks_view.controls.append(task.view)
self.new_task.value = ""
self.view.update()
def delete_task(self, task):
self.tasks.remove(task)
self.tasks_view.controls.remove(task.view)
self.view.update()
def main(page):
page.title = "ToDo App"
page.horizontal_align = "center"
page.update()
# create application instance
app = TodoApp()
# add application's root control to the page
page.add(app.view)
pglet.app("todo-app", target=main)