-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.py
371 lines (298 loc) · 16.5 KB
/
note.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import tkinter as tk
import customtkinter
import sqlite3
import json
from PIL import Image, ImageTk
import tkinter.messagebox as messagebox
import requests
#dev.krwg
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")
CURRENT_VERSION = "1.1.0."
conn = None
cur = None
settings_window = None
current_note_id = None
font_sizes = {"Заголовок": 30, "Подзаголовок": 24, "Обычный": 18}
def db_start():
global conn, cur
try:
conn = sqlite3.connect('notes.db')
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
note TEXT,
pinned INTEGER DEFAULT 0,
formatted_text TEXT
)
""")
conn.commit()
except sqlite3.Error as e:
print(f"Ошибка базы данных: {e}")
customtkinter.CTkMessageBox(title="Ошибка", message="Ошибка подключения к базе данных.")
def save_note(event=None):
global current_note_id
if current_note_id:
note = note_entry.get("1.0", tk.END).strip()
if note:
try:
font_size = selected_font_size.get()
formatted_data = {"text": note, "font_size": font_size}
formatted_text = json.dumps(formatted_data)
cur.execute("UPDATE notes SET note=?, formatted_text=? WHERE id=?", (note, formatted_text, current_note_id))
conn.commit()
update_notes_list()
note_entry.delete("1.0", tk.END)
selected_font_size.set("Обычный")
note_entry.configure(font=("Arial", font_sizes["Обычный"]))
current_note_id = None
except sqlite3.Error as e:
messagebox.showerror("Ошибка", f"Ошибка при сохранении заметки: {e}")
else:
messagebox.showwarning("Предупреждение", "Заметка пуста!")
else:
if event and event.state & 0x0004:
note_entry.insert(tk.END, "\n")
else:
note = note_entry.get("1.0", tk.END).strip()
if note:
try:
font_size = selected_font_size.get()
formatted_data = {"text": note, "font_size": font_size}
formatted_text = json.dumps(formatted_data)
cur.execute("INSERT INTO notes (note, formatted_text) VALUES (?, ?)", (note, formatted_text))
conn.commit()
update_notes_list()
note_entry.delete("1.0", tk.END)
selected_font_size.set("Обычный")
note_entry.configure(font=("Arial", font_sizes["Обычный"]))
except sqlite3.Error as e:
print(f"Ошибка сохранения заметки: {e}")
customtkinter.CTkMessageBox(title="Ошибка", message="Ошибка сохранения заметки.")
else:
customtkinter.CTkMessageBox(title="Предупреждение", message="Заметка пуста!")
def update_notes_list():
global note_container_frame
note_container_frame = customtkinter.CTkFrame(left_frame, fg_color="#333333")
note_container_frame.grid(row=2, column=0, sticky="nsew", padx=10, pady=10)
note_container_frame.columnconfigure(0, weight=1)
for i in range(100):
note_container_frame.rowconfigure(i, weight=1)
try:
cur.execute("SELECT id, note, pinned, formatted_text FROM notes")
notes = cur.fetchall()
if notes:
notes_label.configure(text="")
instruction_label.grid_forget()
for i, (note_id, note_text, pinned, formatted_text) in enumerate(notes):
create_note_container(json.loads(formatted_text), i, note_id, pinned)
else:
notes_label.configure(text="Здесь будут ваши заметки")
instruction_label.grid(row=1, column=0, sticky="w", padx=10, pady=5)
except sqlite3.Error as e:
messagebox.showerror("Ошибка", f"Ошибка при обновлении списка заметок: {e}")
except Exception as e:
messagebox.showerror("Ошибка", f"Непредвиденная ошибка: {e}")
#dev.krwg
def create_note_container(formatted_data, row_index, note_id, pinned):
try:
container = customtkinter.CTkFrame(note_container_frame, fg_color="transparent", corner_radius=5, border_width=2, border_color="#555555")
container.grid(row=row_index, column=0, sticky="nsew", padx=10, pady=(5, 0))
container.columnconfigure(0, weight=1)
try:
label = customtkinter.CTkLabel(container, text=formatted_data["text"], font=("Arial", font_sizes[formatted_data["font_size"]]), text_color="lightgray", anchor="w", justify="left", wraplength=300)
except (KeyError, TypeError):
label = customtkinter.CTkLabel(container, text=formatted_data["text"], font=("Arial", font_sizes["Обычный"]), text_color="lightgray", anchor="w", justify="left", wraplength=300)
label.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
container.rowconfigure(0, weight=1)
menu = tk.Menu(label, tearoff=0, bg="#333333", fg="lightgray")
menu.add_command(label="Удалить", command=lambda note_id=note_id: delete_note_by_id(note_id))
menu.add_command(label="Изменить", command=lambda note_id=note_id: edit_note(note_id))
if pinned:
menu.add_command(label="Открепить", command=lambda note_id=note_id: toggle_pinned(note_id, False))
else:
menu.add_command(label="Закрепить", command=lambda note_id=note_id: toggle_pinned(note_id, True))
label.bind("<Button-3>", lambda event, menu=menu: menu.post(event.x_root, event.y_root))
label.bind("<Button-1>", lambda event, note_text=formatted_data["text"], note_id=note_id: copy_to_editor(note_text, note_id))
if pinned:
container.configure(fg_color="#444444")
add_font_menu(label)
return container
except Exception as e:
messagebox.showerror("Ошибка", f"Ошибка создания контейнера заметки: {e}")
def delete_note_by_id(note_id):
try:
cur.execute("DELETE FROM notes WHERE id=?", (note_id,))
conn.commit()
update_notes_list()
except sqlite3.Error as e:
print(f"Ошибка удаления заметки: {e}")
customtkinter.CTkMessageBox(title="Ошибка", message="Ошибка удаления заметки.")
def copy_to_editor(note_text, note_id):
global current_note_id
note_entry.delete("1.0", tk.END)
note_entry.insert("1.0", note_text)
current_note_id = note_id
def toggle_fullscreen(event=None):
root.attributes('-fullscreen', not root.attributes('-fullscreen'))
def open_settings():
global settings_window, selected_section
if settings_window is None or not settings_window.winfo_exists():
settings_window = customtkinter.CTkToplevel(root)
settings_window.title("Настройки")
settings_window.geometry("800x600")
settings_window.resizable(False, False)
left_settings_column = customtkinter.CTkFrame(settings_window, fg_color="#333333")
left_settings_column.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
right_settings_column = customtkinter.CTkFrame(settings_window, fg_color="#222222")
right_settings_column.grid(row=0, column=1, sticky="nsew", padx=10, pady=10)
settings_sections = {
"Главное": display_main,
"О приложении": display_about
}
for i, (section_name, display_func) in enumerate(settings_sections.items()):
button = customtkinter.CTkButton(left_settings_column, text=section_name,
command=lambda func=display_func, rc=right_settings_column: select_section(func, rc),
fg_color="#333333",
hover_color=("gray80", "gray40"))
button.grid(row=i, column=0, sticky="ew", pady=5)
selected_section = display_about
selected_section(right_settings_column)
settings_window.columnconfigure(0, weight=1)
settings_window.columnconfigure(1, weight=3)
settings_window.protocol("WM_DELETE_WINDOW", close_settings)
def close_settings():
global settings_window
if conn:
conn.close()
settings_window.destroy()
settings_window = None
def check_for_updates():
try:
response = requests.get("https://raw.githubusercontent.com/krwg/JustKeep/refs/heads/master/version.txt", timeout=5)
response.raise_for_status()
latest_version = response.text.strip()
if latest_version > CURRENT_VERSION:
messagebox.showinfo("Обновления", f"Доступно обновление до версии {latest_version}!")
else:
messagebox.showinfo("Обновления", "У вас последняя версия.")
except requests.exceptions.RequestException as e:
messagebox.showerror("Ошибка", f"Ошибка проверки обновлений: {e}. Проверьте интернет соединение.")
except Exception as e:
messagebox.showerror("Ошибка", f"Произошла непредвиденная ошибка: {e}")
def select_section(func, right_column):
global selected_section
selected_section = func
clear_right_column(right_column)
selected_section(right_column)
def clear_right_column(right_column):
for widget in right_column.winfo_children():
widget.grid_remove()
widget.destroy()
def display_about(right_column):
app_name_label = customtkinter.CTkLabel(right_column, text="JustKeep", font=("Arial", 20), text_color="lightgray")
app_name_label.pack(pady=5)
app_desc_label = customtkinter.CTkLabel(right_column, text="Быстрые заметки, без лишних сложностей.", font=("Arial", 14), text_color="lightgray")
app_desc_label.pack(pady=2)
author_label = customtkinter.CTkLabel(right_column, text="krwg. 2024", font=("Arial", 10), text_color="lightgray")
author_label.pack(pady=5)
version_label = customtkinter.CTkLabel(right_column, text=f"Версия: 1.1 Moonstone", font=("Arial", 12))
version_label.pack(pady=5)
check_updates_button = customtkinter.CTkButton(right_column, text="Проверить обновления", command=check_for_updates, fg_color=("gray70", "gray30"), hover_color=("gray80", "gray40"))
check_updates_button.pack(pady=5)
def display_main(right_column):
main_label = customtkinter.CTkLabel(right_column, text="В разработке, ожидайте в новой версии", font=("Arial", 12))
main_label.pack(pady=10)
def add_font_menu(widget):
font_sizes = {"Заголовок": 30, "Подзаголовок": 24, "Обычный": 18}
selected_font_size = tk.StringVar(widget.master, "Обычный")
def change_font_size(size):
try:
widget.configure(font=("Arial", font_sizes[size]))
except KeyError:
customtkinter.CTkMessageBox(title="Ошибка", message="Неверный размер шрифта.")
except Exception as e:
customtkinter.CTkMessageBox(title="Ошибка", message=f"Ошибка изменения шрифта: {e}")
font_menu = tk.Menu(widget, tearoff=0, bg="#333333", fg="lightgray")
for size_name in font_sizes:
font_menu.add_command(label=size_name, command=lambda s=size_name: change_font_size(s))
widget.bind("<Button-3>", lambda event, menu=font_menu: menu.post(event.x_root, event.y_root))
def change_theme(is_dark):
if is_dark:
customtkinter.set_appearance_mode("dark")
else:
customtkinter.set_appearance_mode("light")
def toggle_pinned(note_id, pinned):
try:
cur.execute("UPDATE notes SET pinned=? WHERE id=?", (pinned, note_id))
conn.commit()
update_notes_list()
except sqlite3.Error as e:
print(f"Ошибка изменения статуса закрепления: {e}")
customtkinter.CTkMessageBox(title="Ошибка", message="Ошибка изменения статуса закрепления.")
root = customtkinter.CTk()
root.title("Just Keep")
root.geometry("800x600")
root.resizable(True, True)
try:
root.iconbitmap("icon.ico")
except tk.TclError:
print("Иконка не найдена или некорректный формат.")
header = customtkinter.CTkFrame(root, fg_color="transparent")
header.grid(row=0, column=0, columnspan=2, sticky="ew")
header_label = customtkinter.CTkLabel(header, text="JustKeep", font=("Arial", 24))
header_label.pack(pady=10, side=tk.LEFT, expand=True, fill="x")
#dev.krwg
try:
image = Image.open("settings_icon.png")
photo = ImageTk.PhotoImage(image)
settings_button = customtkinter.CTkButton(header, text="Настройки", image=photo, compound=tk.RIGHT, fg_color=("gray70", "gray30"), hover_color=("gray80", "gray40"), border_width=0, command=open_settings)
settings_button.image = photo
settings_button.pack(side=tk.RIGHT, padx=10, pady=10)
except FileNotFoundError:
print("Файл с иконкой не найден!")
settings_button = customtkinter.CTkButton(header, text="Настройки", command=open_settings, fg_color=("gray70", "gray30"), hover_color=("gray80", "gray40"), border_width=0)
settings_button.pack(side=tk.RIGHT, padx=10, pady=10)
left_frame = customtkinter.CTkFrame(root, fg_color="#333333")
left_frame.grid(row=1, column=0, sticky="nsew")
notes_label = customtkinter.CTkLabel(left_frame, text="Здесь будут ваши заметки", font=("Arial", 14), text_color="lightgray")
notes_label.grid(row=0, column=0, sticky="w", padx=10, pady=10)
instruction_label = customtkinter.CTkLabel(left_frame, text="Для создания введите текст и Enter", font=("Arial", 10), text_color="#888888")
instruction_label.grid(row=1, column=0, sticky="w", padx=10, pady=5)
note_container_frame = customtkinter.CTkFrame(left_frame, fg_color="#333333")
note_container_frame.grid(row=2, column=0, sticky="nsew", padx=10, pady=10)
note_container_frame.columnconfigure(0, weight=1)
for i in range(100):
note_container_frame.rowconfigure(i, weight=1)
right_frame = customtkinter.CTkFrame(root, fg_color="#222222")
right_frame.grid(row=1, column=1, sticky="nsew")
note_label = customtkinter.CTkLabel(right_frame, text="Заметка:", font=("Arial", 12), text_color="lightgray")
note_entry = customtkinter.CTkTextbox(right_frame, font=("Arial", 12), fg_color="#444444", text_color="lightgray")
note_entry.bind("<Return>", save_note)
note_entry.bind("<Control-Return>", lambda event: note_entry.insert(tk.END, "\n"))
note_label.pack(pady=10, padx=10)
note_entry.pack(pady=10, padx=10, fill="both", expand=True)
selected_font_size = tk.StringVar(right_frame, "Обычный")
def change_note_font_size(size):
try:
note_entry.configure(font=("Arial", font_sizes[size]))
selected_font_size.set(size)
except KeyError:
customtkinter.CTkMessageBox(title="Ошибка", message="Неверный размер шрифта.")
except Exception as e:
customtkinter.CTkMessageBox(title="Ошибка", message=f"Ошибка изменения шрифта: {e}")
font_menu_main = tk.Menu(note_entry, tearoff=0, bg="#333333", fg="lightgray")
for size_name in font_sizes:
font_menu_main.add_command(label=size_name, command=lambda s=size_name: change_note_font_size(s))
note_entry.bind("<Button-3>", lambda event, menu=font_menu_main: menu.post(event.x_root, event.y_root))
#dev.krwg
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=3)
root.rowconfigure(1, weight=1)
root.bind("<F11>", toggle_fullscreen)
root.bind("<Escape>", toggle_fullscreen)
db_start()
update_notes_list()
root.protocol("WM_DELETE_WINDOW", lambda: [conn.close(), root.destroy()])
root.mainloop()