-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathadvanced_text_editor.py
233 lines (191 loc) · 8.59 KB
/
advanced_text_editor.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
from Tkinter import *
import tkFileDialog
import os, sys, inspect
import markdown2
main_path= os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
dependencies_path = os.path.join(main_path, 'dependencies')
sys.path.append(dependencies_path)
from tkhtml import *
from tkutil import unbind_destroy
class Window():
def __init__(self, parent):
self.filename =''
self.window = Toplevel(parent)
self.text_box = Text(self.window, background="black", foreground="firebrick", insertbackground="white")
self.text_box.pack(expand = 1, fill= BOTH)
self.text_box.focus_set()
class Editor:
def __init__(self, master):
self.file_name = ""
self.html_window=""
#self.html_viewer=""
initial_text_box = Text(root, background="black", foreground="firebrick", insertbackground="white")
initial_text_box.pack(expand = 1, fill= BOTH)
initial_text_box.focus_set()
initial_text_box.insert(END, """This is a text editor made by Luke Carlson (github.com/jLukeC).""")
self.file_opt = options = {}
# options for opening files
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt'), ('markdown', '.md'), ('html', '.html')]
options['initialdir'] = os.path
options['initialfile'] = 'myfile.txt'
options['parent'] = root
options['title'] = 'This is a title'
# defining options for opening a directory
self.dir_opt = options = {}
options['initialdir'] = os.path
options['mustexist'] = False
options['parent'] = root
options['title'] = 'This is a title'
def find_focus():
focus= root.focus_get()
print focus
print focus.get(1.0, END)
print focus.master
focus.master.wm_title("focused")
menubar = Menu(root)
menubar.add_command(label="Hello!", command=find_focus)
menubar.add_command(label="fds!", command=find_focus)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=self.new_window, accelerator="Command+N")
filemenu.add_command(label="Open", command=self.open_file, accelerator="Command+O")
filemenu.add_command(label="Save", command=self.save_file, accelerator="Command+S")
filemenu.add_command(label="Save as...", command=self.save_as_file)
filemenu.add_separator()
filemenu.add_command(label="Close Window", command=self.destroy, accelerator="Command+W")
filemenu.add_command(label="Exit", command=self.quit_project, accelerator="Command+Q")
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=find_focus)
editmenu.add_separator()
editmenu.add_command(label="Cut", command=self.cut, accelerator="Command+X")
editmenu.add_command(label="Copy", command=self.copy, accelerator="Command+C")
editmenu.add_command(label="Paste", command=self.paste, accelerator="Command+V")
editmenu.add_command(label="Select All", command=self.select_all, accelerator="Command+A")
editmenu.add_command(label="Delete", command=self.delete_selection)
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Find Focus", command=find_focus)
helpmenu.add_command(label="About", command=self.about)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
root.bind_all("<Command-,>", self.launch_html_viewer)
root.bind_all("<Command-m>", self.view_html)
root.bind_all("<Command-n>", self.new_window)
root.bind_all("<Command-o>", self.open_file)
root.bind_all("<Command-s>", self.save_file)
root.bind_all("<Command-Shift-s>", self.save_as_file) #doesnt work
root.bind_all("<Command-a>", self.select_all)
root.bind_all("<Command-w>", self.destroy)
root.bind_all("<Command-q>", self.quit_project)
def markdowner(self, event=''):
focus=root.focus_get()
print markdown2.markdown(focus.get(1.0, END))
def view_html(self, event=''):
focus=root.focus_get()
file_extension = os.path.splitext(focus.master.title())[1]
print file_extension
if (file_extension == ".md"): #checks if it is markdown
# html = markdown2.markdown(focus.get(1.0, END))
# print html
try:
html = markdown2.markdown(focus.get(1.0, END))
print html
save_file = open(os.path.splitext(focus.master.title())[0] + ".html", 'w')
save_file.write(html)
print save_file.name
self.launch_html_viewer(url=save_file.name)
except:
print "couldnt convert markdown to html"
else:
self.launch_html_viewer()
def launch_html_viewer(self, event='', url=''):
print url
focus=root.focus_get()
if url == '':
url = focus.master.title()
if self.html_window == '':
self.html_window=Toplevel(root)
self.html_window.wm_title("HTML Viewer")
self.html_viewer=tkHTMLViewer(self.html_window)
self.html_viewer.url=os.path.basename(url)
try:
self.html_viewer.display(url)# for local: os.path.basename(url)
print url + "viewed"
except:
print "html section couldnt work"
else:
# self.html_viewer.display(url)
try:
self.html_viewer.display(url)
except:
print "html section couldnt work"
def open_file(self, event=''):
open_file = tkFileDialog.askopenfile(mode='r', **self.file_opt)
window_app = Window(root)
window_app.text_box.delete(1.0, END)
window_app.text_box.insert(END, open_file.read())
window_app.file_name = open_file.name
window_app.window.wm_title(window_app.file_name)
print window_app.file_name
def save_file(self, event=''):
focus=root.focus_get()
if (focus.master.title() == '' or focus.master.title() == "Luke's Text Editor"):
self.save_as_file()
else:
save_file = open(focus.master.wm_title(), 'w')
save_file.write(focus.get(1.0, END))
def save_as_file(self, event=''):
focus=root.focus_get()
save_file = tkFileDialog.asksaveasfile(mode='w', **self.file_opt)
save_file.write(focus.get(1.0, END))
focus.master.wm_title(save_file.name)
print focus.master.title()
def copy(self):
focus=root.focus_get()
root.clipboard_clear()
root.clipboard_append(focus.selection_get())
def cut(self):
self.copy()
self.delete_selection()
def paste(self):
focus=root.focus_get()
result = root.selection_get(selection = "CLIPBOARD")
focus.insert(INSERT, result)
def delete_selection(self):
focus=root.focus_get()
focus.delete(SEL_FIRST, SEL_LAST)
def select_all(self, event=''):
focus=root.focus_get()
focus.tag_add("sel","1.0","end")
def new_window(self, event=''):
self.window_app = Window(root)
def about(self):
about_window = Toplevel(root)
about_window.wm_title("About")
info = Label(about_window, text="""This is a text editor made by Luke Carlson (github.com/jLukeC) over a few days in summer 2013.""")
info.pack()
def destroy(self, event=''):
focus=root.focus_get()
focus_parent_string=focus.winfo_parent()
focus_parent = root.nametowidget(focus_parent_string)
unbind_destroy(focus_parent)
focus_parent.wm_withdraw()
try:
focus_parent.wm_withdraw()
except: pass
try:
focus_parent.destroy()
except: pass
def quit_project(self):
sys.exit()
if __name__=='__main__':
root = Tk()
root.wm_title("Luke's Text Editor")
htmlw = Toplevel(root)
htmlv = tkHTMLViewer(htmlw)
htmlv.display("/Users/Luke/programming/projects/advanced-text-editor/myfile.html")
app = Editor(root)
root.mainloop()
# great colors list http://wiki.tcl.tk/16166
# thank you http://www.pysol.org/ for the html viewer code (and the destroy code)