-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
212 lines (182 loc) · 8.32 KB
/
main.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
import os
import sys
import tkinter as tk
from tkinter import filedialog
import tkinter.messagebox as mbox
from widgets import Menubar, Textbox, Statusbar
from toplevels import FontWindow, ColorsWindow
from configparser import ConfigParser
APP_NAME = 'TextPad'
NEW_FILE_NAME = 'Untitled'
ICON_PATH = 'icon.ico'
INI_FILE_PATH = 'config.ini'
FILEDIALOG_FILETYPES = [('Text Documents', '*.txt'), ('All Files', '*.*')]
class App():
def __init__(self, master):
# Initialize config parser
self.config_data = ConfigParser()
self.config_data.read('config.ini')
self.opened_file = None
self.autosave_after_id = None
self.autosave_interval = int(self.config_data.get('AutoSave', 'interval'))
self.icon_path = ICON_PATH
self.master = master
master.title(f'{NEW_FILE_NAME} - {APP_NAME}')
master.geometry(self.config_data.get('Window', 'geometry'))
# Widgets
self.textbox = Textbox(app=self)
self.statusbar = Statusbar(master)
self.menu_bar = Menubar(app=self)
self.textbox.scrollbar = tk.Scrollbar(self.textbox, command=self.textbox.yview)
self.textbox.config(yscrollcommand=self.textbox.scrollbar.set)
self.textbox.h_scrollbar = tk.Scrollbar(self.textbox, command=self.textbox.xview, orient='horizontal')
self.textbox.config(xscrollcommand=self.textbox.h_scrollbar.set)
self.context_menu = tk.Menu(master, tearoff=0)
self.context_menu.add_command(label="Undo", command=self.textbox.edit_undo)
self.context_menu.add_separator()
self.context_menu.add_command(label="Cut", command=self.textbox.cut_text)
self.context_menu.add_command(label="Copy", command=self.textbox.copy_text)
self.context_menu.add_command(label="Paste", command=self.textbox.paste_text)
self.context_menu.add_command(label="Delete", command=self.textbox.delete_text)
self.context_menu.add_separator()
self.context_menu.add_command(label="Select All", command=self.textbox.select_all)
self.context_menu.add_command(label="Time/Date", command=self.textbox.insert_time)
# Layout
self.textbox.pack(expand=True, fill='both')
self.textbox.scrollbar.pack(side='right', fill='y')
self.statusbar.pack(side='bottom', fill='x')
# Key binding & on close command
self.master.protocol('WM_DELETE_WINDOW', self.on_close)
self.master.bind('<Control-n>', lambda e: self.new_file())
self.master.bind('<Control-o>', lambda e: self.open_file())
self.master.bind('<Control-s>', lambda e: self.save_file())
self.master.bind('<Control-S>', lambda e: self.save_file_as())
self.textbox.bind('<F5>', lambda e: self.textbox.insert_time())
self.textbox.bind('<Button-3>', lambda e: self.show_context_menu(e))
self.textbox.bind('<Control-Key-x>', lambda e: self.textbox.cut_text())
self.textbox.bind('<Control-Key-c>', lambda e: self.textbox.copy_text())
self.textbox.bind('<Control-Key-v>', lambda e: self.textbox.paste_text())
self.textbox.bind('<Control-Key>-', lambda e: self.textbox.change_size(-2))
self.textbox.bind('<Control-=>', lambda e: self.textbox.change_size(2))
self.master.bind('<F1>', lambda e: self.view_shortcuts())
if self.autosave_var.get():
self.statusbar.print(f'AUTOSAVE: ON ({int(self.autosave_interval) / 1000}s)')
self.textbox.focus()
# last because of the render issue
self.master.iconbitmap(self.icon_path)
def new_file(self):
self.autosave(stop=True)
if self.save_changes_answer() is None:
return
self.opened_file = None
self.master.title(f'{NEW_FILE_NAME} - {APP_NAME}')
self.textbox.delete(1.0, 'end')
self.statusbar.print('NEW FILE')
def open_file(self):
if self.save_changes_answer() is None:
return
file = filedialog.askopenfilename(file=FILEDIALOG_FILETYPES)
try:
with open(file, 'r', encoding='utf-8') as f:
file_content = f.read()
except:
self.statusbar.print('COULDN\'T READ FILE')
else:
# SUCCESS
self.opened_file = file
self.master.title(f'{os.path.basename(file)} - {APP_NAME}')
self.textbox.delete(1.0, 'end')
self.textbox.insert(1.0, file_content)
self.statusbar.print('READY')
self.autosave()
def save_file(self):
if self.opened_file:
try:
with open(self.opened_file, 'w', encoding='utf-8') as f:
f.write(self.textbox.get(1.0, 'end')[:-1])
except:
self.statusbar.print('COULDN\'T RESAVE FILE')
else:
if not self.autosave_after_id:
self.statusbar.print('SAVED')
else:
self.save_file_as()
def save_file_as(self):
file = filedialog.asksaveasfilename(initialfile=NEW_FILE_NAME, defaultextension='.txt', filetypes=FILEDIALOG_FILETYPES)
if file: # file dialog not closed
try:
with open(file, 'w', encoding='utf-8') as f:
f.write(self.textbox.get(1.0, 'end')[:-1])
except:
self.statusbar.print('COULDN\'T SAVE FILE')
else:
# SUCCESS
self.opened_file = file
self.master.title(f'{os.path.basename(file)} - {APP_NAME}')
self.statusbar.print(f'SAVED at {file}')
self.autosave()
def show_context_menu(self, event):
self.context_menu.tk_popup(event.x_root, event.y_root)
def open_font_window(self):
FontWindow(app=self)
def open_colors_window(self):
ColorsWindow(app=self)
def view_shortcuts(self):
lst = [
'<Ctrl+N> - New File', '<Ctrl+O> - Open file', '<Ctrl-S> - Save file',
'<Ctrl-Shift-S> - Save As', '', '<Ctrl-Z> - Undo', '<Ctrl-Y> - Redo', '<Ctrl-X> - Cut text',
'<Ctrl-C> - Copy text', '<Ctrl-V> - Paste text', '<Del> - Delete selected text',
'<Ctrl-A> - Select all text', '<F5> - Insert Time/Date', '', '<Ctrl-Plus> - Enlarge text font',
'<Ctrl-Minus> - Reduce text font', '', '<F1> - View shortcuts'
]
mbox.showinfo('Shortcut List', '\n'.join(lst))
def change_autosave(self):
state = self.autosave_var.get()
self.autosave(stop=not state)
self.statusbar.print(f'AUTOSAVE: {f"ON ({int(self.autosave_interval) / 1000}s)" if state else "OFF"}')
def autosave(self, stop=False):
if stop is True:
if self.autosave_after_id:
self.master.after_cancel(self.autosave_after_id)
else:
if self.autosave_var.get() and self.opened_file:
self.save_file()
self.autosave_after_id = self.master.after(self.autosave_interval, self.autosave)
def save_changes_answer(self):
text = self.textbox.get(1.0, 'end')[:-1]
answer = False
# New file and not empty
if not self.opened_file and text != '':
answer = mbox.askyesnocancel(APP_NAME, f'Do you want to SAVE CHANGES to {NEW_FILE_NAME}?')
if answer is True:
self.save_file_as()
elif self.opened_file:
with open(self.opened_file, 'r') as f:
file_text = f.read()
if text != file_text:
answer = mbox.askyesnocancel(APP_NAME, f'Do you want to SAVE CHANGES to {self.opened_file}?')
if answer is True:
self.save_file()
return answer
def on_close(self):
if self.save_changes_answer() is None:
return
# in case the autosave interval is long
self.autosave()
self.config_data.set('Window', 'geometry', self.master.geometry())
self.config_data.set('AutoSave', 'state', str(self.autosave_var.get()))
# Save CONFIG data
try:
with open(INI_FILE_PATH, 'w') as file:
self.config_data.write(file)
except:
print('Config data was NOT written!')
else:
print('Config data SAVED!')
sys.exit()
def main():
root = tk.Tk()
App(root)
root.mainloop()
if __name__ == '__main__':
main()