-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun2.py
310 lines (231 loc) · 8.2 KB
/
run2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from tkinter import *
import os
from tkinter import messagebox as msg
root = Tk()
root.geometry("450x200")
# FUNCTIONS
def import_from_file(file_name):
file = open(file_name, "r")
global list_of_tasks
list_of_tasks = []
for line in file.readlines():
list_of_tasks.append(line.strip())
file.close()
def show_tasks():
import_from_file('tasks.txt')
listbox.delete(0, END)
for task in list_of_tasks:
listbox.insert(END, task)
def check_in_list(name):
if name not in list_of_tasks:
msg.showerror("Error", "There is no such task in the list")
else:
pass
def find_in_listbox():
import_from_file('tasks.txt')
index = 0
task_name = entry_var.get().strip()
check_in_list(task_name)
for i, elem in enumerate(list_of_tasks):
elem.strip()
if elem == task_name:
index = i
listbox.see(index)
def remove_task(): # wyjątek jeśli nie ma zadania
import_from_file('tasks.txt')
task_name = entry_var.get()
check_in_list(task_name)
for task in list_of_tasks:
if task == task_name:
list_of_tasks.remove(task)
file = open('tasks.txt', 'w')
for task in list_of_tasks:
file.write(task+"\n")
file.close()
path = "./files/" + task_name + ".txt"
try:
os.remove(path)
except FileNotFoundError:
msg.showerror("ERROR", "File: {} does not exists".format(task_name))
show_tasks()
def add_task():
file = open('tasks.txt', 'a+')
new_task = entry_task_name_var.get()
file.write(new_task+'\n')
file.close()
global description
description = task_description.get(1.0, END)
file = open('files/{}.txt'.format(new_task), 'a+')
file.writelines(description)
file.close()
show_tasks()
window.destroy()
def show_adding_window():
global window
window = Tk()
window.title('Add a new task')
window.geometry('300x200')
global enter_name_label # label do task name'a
enter_name_label = Label(window, text='Enter here task name: ')
enter_name_label.pack()
enter_name_label.place(x=10, y=10)
global entry_task_name_var # zmienna przechwytująca to co jest w entry
entry_task_name_var = StringVar(master=window)
global entry_task_name # pole do wpisania nazwy zadania
entry_task_name = Entry(window, textvariable=entry_task_name_var)
entry_task_name.pack()
entry_task_name.place(x=10, y=30)
global task_description_label # label do pola na opis zadania
task_description_label = Label(window, text='Enter here task description: ')
task_description_label.pack()
task_description_label.place(x=10, y=50)
global task_description # pole opisowe zadania
task_description = Text(window, width=130, height=280,)
task_description.pack()
task_description.place(x=10, y=70)
global plus # przycisk dodający zadanie
plus = Button(window, text='add task', command=add_task)
plus.pack()
plus.place(x=220, y=10)
window.mainloop()
def close_confirming():
window_c.destroy()
def clear_all_tasks():
open('tasks.txt', 'w').close()
for file in os.listdir('files'):
try:
os.remove(file)
except FileNotFoundError:
print("nie odnaleziono pliku")
show_tasks()
window_c.destroy()
def show_confirming_window():
global window_c
window_c = Tk()
window_c.geometry("300x100")
window_c.title('Delete all tasks')
global info
info = Label(window_c, text='This operation will delete all the tasks from your list')
info.pack()
#info.place(x=10, y=10)
global cancel_button
cancel_button = Button(window_c, text='Cancel', command=close_confirming)
cancel_button.pack()
cancel_button.place(x=100, y=30)
global confirm_button
confirm_button = Button(window_c, text='Confirm', command=clear_all_tasks)
confirm_button.pack()
confirm_button.place(x=150, y=30)
window_c.mainloop()
def confirm_editing():
task_name = entry_new_task_name_var.get()
path1 = "./files/" + entry_var.get() + ".txt"
path2 = "./files/" + task_name + ".txt"
os.rename(path1, path2)
global new_description
new_description = new_task_description.get(1.0, END)
file = open(path2, "w+")
file.write(new_description)
file.close()
window_e.destroy()
def edit_task():
task_name = entry_var.get()
if task_name not in list_of_tasks:
msg.showerror("Error", "There is no such task in the list")
else:
global window_e
window_e = Tk()
window_e.geometry('300x200')
global enter_new_name_label
enter_new_name_label = Label(window_e, text='Enter here new task name: ')
enter_new_name_label.pack()
enter_new_name_label.place(x=10, y=10)
global entry_new_task_name_var
entry_new_task_name_var = StringVar(window_e)
global entry_new_task_name
entry_new_task_name = Entry(window_e, textvariable=entry_new_task_name_var)
entry_new_task_name.pack()
entry_new_task_name.place(x=10, y=30)
global new_task_description_label
new_task_description_label = Label(window_e, text='Enter here task description: ')
new_task_description_label.pack()
new_task_description_label.place(x=10, y=50)
global new_task_description
new_task_description = Text(window_e, width=130, height=280)
new_task_description.pack()
new_task_description.place(x=10, y=70)
global edit_e
edit_e = Button(window_e, text='edit task', command=confirm_editing)
edit_e.pack()
edit_e.place(x=220, y=10)
entry_new_task_name_var.set(task_name)
file_path = "./files/" + task_name + ".txt"
file = open(file_path, "r")
for line in file.readlines():
new_task_description.insert(END, line)
file.close()
window_e.mainloop()
def listbox_select(index):
entry_var.set(listbox.get(listbox.curselection()))
# WIDGETS
# Entry
label_var = StringVar(root, 'Find in the list or type here: ')
label = Label(root, textvariable=label_var)
label.pack()
label.place(x=10, y=10)
entry_var = StringVar(root)
entry_var.set('type task name...')
edit = Entry(root, textvariable=entry_var)
edit.pack()
edit.place(x=10, y=30, width=150)
# Listbox
list_label = Label(root, text='Here is your list of tasks')
list_label.pack()
list_label.place(x=10, y=60)
listbox = Listbox(root, width=50, height=6)
listbox.pack()
listbox.place(x=10, y=80)
# MENUS
mainmenu = Menu()
root.config(menu=mainmenu)
opt_menu = Menu(mainmenu)
mainmenu.add_cascade(label='Options', menu=opt_menu)
settings_menu = Menu(mainmenu)
mainmenu.add_cascade(label='Settings', menu=settings_menu)
opt_menu.add_command(label='Add task', command=show_adding_window)
opt_menu.add_command(label='Remove task', command=remove_task)
opt_menu.add_command(label='Edit task', command=edit_task)
opt_menu.add_separator()
opt_menu.add_command(label='Show tasks in text window')
opt_menu.add_separator()
opt_menu.add_command(label='clear all tasks', command=show_confirming_window)
# Opis zadań, pole textowe przy dodawaniu zadań
'''
Tutorial(menu) jak korzystać z menagera,
messagebox zaimportować
obsługa wyjątków
(przy entry, jeśli nie ma takiego zadania w liście, lub pliku)
'''
listbox.bind('<<ListboxSelect>>', listbox_select)
# BUTTONS
# Require new, confirming window
add_task_button = Button(root, text='new task', command=show_adding_window)
clear_all_tasks_button = Button(root, text='clear tasks list', command=show_confirming_window) # okno potwierdzające
# Choose from the list
find_button = Button(root, text='find', command=find_in_listbox)
remove_task_button = Button(root, text='remove task', command=remove_task)
edit_task_button = Button(root, text='edit task', command=edit_task) # okno z polem tekstowym jak przy dodawaniu zadań
# packing
add_task_button.pack()
remove_task_button.pack()
edit_task_button.pack()
clear_all_tasks_button.pack()
find_button.pack()
# placing
add_task_button.place(x=325, y=10, width=100)
remove_task_button.place(x=325, y=40, width=100)
edit_task_button.place(x=325, y=70, width=100)
clear_all_tasks_button.place(x=325, y=100, width=100)
find_button.place(x=165, y=25)
show_tasks()
root.mainloop()