-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo.py
89 lines (67 loc) · 2.59 KB
/
todo.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
from collections import OrderedDict
from turbosnake import functional_component, use_ref, use_toggle, use_state, use_callback_proxy
from turbosnake.ttk import tk_app
from turbosnake.ttk import tk_entry, tk_button, tk_window, tk_label, tk_packed_frame, tk_scrollable_frame, tk_link
@functional_component
def create_form(on_create, on_dismiss):
input_ref = use_ref()
@use_callback_proxy
def create():
on_create(input_ref.current.text)
on_dismiss()
@use_callback_proxy
def create_and_continue():
on_create(input_ref.current.text)
input_ref.current.text = ''
with tk_window(on_close=on_dismiss, resizable=False, min_width=300, title='Create a TODO'):
tk_entry(ref=input_ref, initial_value='Procrastinate', fill='x', px=8, py=8)
with tk_packed_frame(default_side='left'):
tk_button(text='Create', on_click=create)
tk_button(text='Create+', on_click=create_and_continue)
@functional_component
def todo_item(name, text, on_done):
@use_callback_proxy
def done():
on_done(name)
with tk_packed_frame(default_side='left', fill='x', anchor='n'):
tk_button(text='Done!', on_click=done, fill='y')
with tk_packed_frame(expand=True, fill='x'):
tk_label(text=name, anchor='w')
tk_label(text=text, anchor='w')
INITIAL_LIST = OrderedDict({
'TODO-0': 'Make this example look better',
})
@functional_component
def root():
create_open, toggle_create_open = use_toggle()
items, set_items = use_state(INITIAL_LIST)
counter, set_counter = use_state(1)
@use_callback_proxy
def create(new_text):
new_items = items.copy()
new_items[f'TODO-{counter}'] = new_text
set_counter(counter + 1)
set_items(new_items)
@use_callback_proxy
def done(item_name):
new_items = items.copy()
del new_items[item_name]
set_items(new_items)
if create_open:
create_form(
on_dismiss=toggle_create_open,
on_create=create
)
tk_button(text='Add', on_click=toggle_create_open, disabled=create_open)
with tk_scrollable_frame(fill='both', expand=1):
for name, text in items.items():
todo_item(key=name, text=text, name=name, on_done=done)
with tk_packed_frame(default_side='left'):
tk_label(text='Achieved with')
tk_link(
text='turbosnake',
href='https://github.com/AlexeyBond/turbosnake'
)
if __name__ == '__main__':
with tk_app(min_width=300, min_height=400, resizable_w=False, title='TODO list'):
root()