-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlh_notepad.py
More file actions
156 lines (127 loc) · 4.26 KB
/
Copy pathmlh_notepad.py
File metadata and controls
156 lines (127 loc) · 4.26 KB
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
"""
A very basic text-editor with undo/redo capabilities and
file open/save/save-as functions. A tkinter experiment, nothing more...
Copyright (C) 2022 Mohammad L. Hussain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog as tkfd
import tkinter.messagebox as tkm
from time import sleep
root = tk.Tk()
T = tk.Text(undo=True)
F = ttk.Frame()
F.grid(row=0,column=0,sticky='ns')
T.grid(row=0,column=1,sticky='nsew')
root.columnconfigure(1, minsize=800, weight=1)
root.rowconfigure(0, minsize=600, weight=1)
btn_new = ttk.Button(F, text="New")
btn_open = ttk.Button(F,text="Open")
btn_save = ttk.Button(F,text="Save")
btn_saveas = ttk.Button(F,text="Save As")
btn_new.grid(row=0,column=0, pady=5)
btn_open.grid(row=1,column=0, pady=5)
btn_save.grid(row=2,column=0, pady=5)
btn_saveas.grid(row=3,column=0,pady=5)
F['borderwidth'] = 8
BASE_TITLE = "MLH Text Editor"
root.title(BASE_TITLE + " - Unsaved")
current_file = ""
def new_file(evt=None):
global current_file
if T.count(1.0,'end')[0] > 1 and T.edit_modified():
save_first = tkm.askyesnocancel("Save Current File?",
"Would you like to save changes to the current file first?")
if save_first and not current_file:
file_saved = save_file_as()
if not file_saved:
return
elif save_first:
save_current_file()
elif save_first is None:
return
T.delete(1.0,'end')
T.edit_reset()
root.title(BASE_TITLE + " - Unsaved")
current_file = ""
def open_file(evt=None):
global current_file
if T.count(1.0,'end')[0] > 1 and T.edit_modified():
save_first = tkm.askyesnocancel("Save Current File?",
"Would you like to save changes to the current file first?")
if save_first and not current_file:
file_saved = save_file_as()
if not file_saved:
return
sleep(0.2)
elif save_first:
save_current_file()
elif save_first == None:
return
fpath = tkfd.askopenfilename(
filetypes=[("Text Files", "*.txt"),("All Files", "*")])
if not fpath:
return
with open(fpath, 'r') as file:
T.delete(1.0,'end')
text=file.read()
T.insert('end', text)
T.edit_reset()
root.title(BASE_TITLE + " - " + file.name)
current_file = fpath
def save_current_file(evt=None):
with open(current_file, 'w') as output_file:
text = T.get(1.0,'end')
output_file.write(text)
def save_file_as(evt=None):
global current_file
fpath = tkfd.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files","*.txt"),("All Files","*.*")])
if not fpath:
return False
with open(fpath, "w") as output_file:
text = T.get(1.0,'end')
output_file.write(text)
root.title(BASE_TITLE + " - " + fpath)
current_file = fpath
return True
def save_file(evt=None):
if current_file:
save_current_file()
else:
save_file_as()
def select_all(evt=None):
T.tag_add('sel',1.0,'end')
return 'break'
def do_undo(evt=None):
try:
T.edit_undo()
except:
pass
def do_redo(evt=None):
try:
T.edit_redo()
except:
pass
btn_new['command'] = new_file
btn_open['command'] = open_file
btn_save['command'] = save_file
btn_saveas['command'] = save_file_as
T.bind("<Control-a>", select_all)
T.bind("<Control-Shift-S>", save_file_as)
T.bind("<Control-s>", save_file)
T.bind("<Control-o>", open_file)
T.bind("<Control-n>", new_file)
T.bind("<Control-z>", do_undo)
T.bind("<Control-Shift-Z>", do_redo)
root.mainloop()