-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
295 lines (235 loc) · 10 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
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
import tkinter as tk
from tkinter import filedialog, Text, Menu
# Main Window
root = tk.Tk()
root.title("Python Text Editor")
# Features
class LineNumberCanvas(tk.Canvas):
def __init__(self, *args, **kwargs):
tk.Canvas.__init__(self, *args, **kwargs)
self.textwidget = None
def attach(self, text_widget):
self.textwidget = text_widget
def redraw(self, *args):
'''Redraw line numbers'''
self.delete("all")
i = self.textwidget.index("@0,0")
while True :
dline= self.textwidget.dlineinfo(i)
if dline is None: break
y = dline[1]
linenum = str(i).split(".")[0]
self.create_text(2,y,anchor="nw", text=linenum)
i = self.textwidget.index("%s+1line" % i)
def toggle_line_numbers():
if line_numbers.winfo_ismapped():
line_numbers.pack_forget()
text.pack(side="right", fill="both", expand=True) # Make sure the text widget expands to fill the space
else:
line_numbers.pack(side="left", fill="y")
text.pack(side="right", fill="both", expand=True) # Re-pack the text widget to maintain the layout
line_numbers.redraw() # Redraw the line numbers after showing the canvas
def new_file():
text.delete(1.0, tk.END)
line_numbers.redraw() # Reset Numbers
def open_file():
file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
if file_path:
with open(file_path, "r") as file:
text.delete(1.0, tk.END)
text.insert(tk.END, file.read())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
if file_path:
with open(file_path, "w") as file:
file.write(text.get(1.0, tk.END))
def select_all():
text.tag_add('sel', '1.0', 'end')
def copy():
if text.tag_ranges('sel'):
text.clipboard_clear()
text.clipboard_append(text.get(tk.SEL_FIRST, tk.SEL_LAST))
def paste():
try:
selected_text = text.selection_get(selection='CLIPBOARD')
if text.tag_ranges('sel'):
# If there's selected text, replace it with clipboard contents
start = text.index(tk.SEL_FIRST)
end = text.index(tk.SEL_LAST)
text.delete(start, end)
text.insert(start, selected_text)
else:
# If there's no selected text, just insert at the current position
text.insert(tk.INSERT, selected_text)
except tk.TclError:
pass # Catch the exception if there's no text in the clipboard
def cut():
if text.tag_ranges('sel'):
copy()
text.delete(tk.SEL_FIRST, tk.SEL_LAST)
# Custom Find and Replace Dialog Box
class FindReplaceDialog(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.title("Find and Replace")
# Find field
self.find_label = tk.Label(self, text="Find:")
self.find_label.grid(row=0, column=0, padx=10, pady=5)
self.find_entry = tk.Entry(self)
self.find_entry.grid(row=0, column=1, padx=10, pady=5)
# Replace field
self.replace_label = tk.Label(self, text="Replace with:")
self.replace_label.grid(row=1, column=0, padx=10, pady=5)
self.replace_entry = tk.Entry(self)
self.replace_entry.grid(row=1, column=1, padx=10, pady=5)
# Replace button
self.replace_button = tk.Button(self, text="Replace", command=self.replace)
self.replace_button.grid(row=2, column=0, padx=10, pady=5)
# Cancel button
self.cancel_button = tk.Button(self, text="Cancel", command=self.destroy)
self.cancel_button.grid(row=2, column=1, padx=10, pady=5)
def replace(self):
find_text = self.find_entry.get()
replace_text = self.replace_entry.get()
if find_text and replace_text:
content = text.get(1.0, tk.END)
new_content = content.replace(find_text, replace_text)
text.delete(1.0, tk.END)
text.insert(1.0, new_content)
self.destroy()
def open_find_replace_dialog():
dialog = FindReplaceDialog(root)
root.wait_window(dialog) # This will wait until the dialog window is closed.
def add_shortcut(key, func):
root.bind(key, lambda event: func())
# Theme Based Methods
def toggle_theme():
global current_mode
modes = ['light', 'dark', 'sepia', 'high_contrast']
mode_index = modes.index(current_mode) + 1
if mode_index >= len(modes):
mode_index = 0
current_mode = modes[mode_index]
color_scheme = globals()[f"{current_mode}_mode"]
def set_light_mode():
global current_mode
current_mode = "light"
apply_theme(light_mode)
def set_dark_mode():
global current_mode
current_mode = "dark"
apply_theme(dark_mode)
def set_sepia_mode():
global current_mode
current_mode = "sepia"
apply_theme(sepia_mode)
def set_high_contrast_mode():
global current_mode
current_mode = "high_contrast"
apply_theme(high_contrast_mode)
# Color schemes
light_mode = {
"text_bg": "#FFFFFF", # White background for the text area
"text_fg": "#000000", # Black text color
"sidebar_bg": "#F0F0F0", # Light background for line numbers
"sidebar_fg": "#000000", # Black color for line numbers
"menu_bg": "#F0F0F0", # Light background for the menu
"menu_fg": "#000000", # Black text color for the menu
}
dark_mode = {
"text_bg": "#333333", # Dark background for the text area
"text_fg": "#CCCCCC", # Light grey text color
"sidebar_bg": "#1E1E1E", # Dark background for line numbers
"sidebar_fg": "#FFFFFF", # White color for line numbers
"menu_bg": "#1E1E1E", # Dark background for the menu
"menu_fg": "#CCCCCC", # Light grey text color for the menu
}
sepia_mode = {
"text_bg": "#F4ECE3", # Warm light background for the text area
"text_fg": "#5B4636", # Dark brown text color
"sidebar_bg": "#E4DCCF", # Light sepia background for line numbers
"sidebar_fg": "#5B4636", # Dark brown color for line numbers
"menu_bg": "#E4DCCF", # Light sepia background for the menu
"menu_fg": "#5B4636", # Dark brown text color for the menu
}
high_contrast_mode = {
"text_bg": "#000000", # Black background for the text area
"text_fg": "#FFFFFF", # White text color
"sidebar_bg": "#FFFFFF", # White background for line numbers
"sidebar_fg": "#000000", # Black color for line numbers
"menu_bg": "#000000", # Black background for the menu
"menu_fg": "#FFFFFF", # White text color for the menu
}
# Current mode starts as dark mode
current_mode = "light"
def apply_theme(color_scheme):
global current_mode
# Apply color scheme to text widget and line numbers
text.config(bg=color_scheme["text_bg"], fg=color_scheme["text_fg"], insertbackground=color_scheme["text_fg"])
line_numbers.config(bg=color_scheme["sidebar_bg"], fg=color_scheme["sidebar_fg"])
# Update the colors for the menu and all of its children
menu.config(bg=color_scheme["menu_bg"], fg=color_scheme["menu_fg"])
for menu_item in menu.winfo_children():
menu_item.config(bg=color_scheme["menu_bg"], fg=color_scheme["menu_fg"])
# Bind shortcuts to these functions
add_shortcut('<Control-n>', new_file)
add_shortcut('<Control-o>', open_file)
add_shortcut('<Control-s>', save_file)
add_shortcut('<Control-a>', select_all)
add_shortcut('<Control-f>', open_find_replace_dialog)
add_shortcut('<Control-x>', cut)
add_shortcut('<Control-c>', copy)
add_shortcut('<Control-v>', paste)
add_shortcut('<Control-l>', toggle_line_numbers)
# File Menu
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file, accelerator="Ctrl+N")
file_menu.add_command(label="Open", command=open_file, accelerator="Ctrl+O")
file_menu.add_command(label="Save", command=save_file, accelerator="Ctrl+S")
file_menu.add_separator()
file_menu.add_command(label="Toggle Line Numbers", command=toggle_line_numbers, accelerator="Ctrl+L")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# Edit Menu
edit_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Undo", command=lambda: text.edit_undo(), accelerator="Ctrl+Z")
edit_menu.add_command(label="Redo", command=lambda: text.edit_redo(), accelerator="Ctrl+Y")
edit_menu.add_separator()
edit_menu.add_command(label="Cut", command=cut, accelerator="Ctrl+X")
edit_menu.add_command(label="Copy", command=copy, accelerator="Ctrl+C")
edit_menu.add_command(label="Paste", command=paste, accelerator="Ctrl+V")
edit_menu.add_separator()
edit_menu.add_command(label="Select All", command=select_all, accelerator="Ctrl+A")
edit_menu.add_command(label="Find and Replace", command=open_find_replace_dialog, accelerator="Ctrl+F")
# View Menu
view_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="View", menu=view_menu)
# view_menu.add_command(label="Toggle Dark Mode", command=toggle_dark_mode)
# Theme Menu
theme_menu = tk.Menu(menu, tearoff=0) # Create a new menu for themes
menu.add_cascade(label="Theme", menu=theme_menu)
theme_menu.add_command(label="Light Mode", command=set_light_mode)
theme_menu.add_command(label="Dark Mode", command=set_dark_mode)
theme_menu.add_command(label="Sepia Mode", command=set_sepia_mode)
theme_menu.add_command(label="High Contrast Mode", command=set_high_contrast_mode)
# Line Numbers
line_numbers = LineNumberCanvas(root, width=30)
line_numbers.pack(side="left", fill="y")
# Text Area
text = tk.Text(root, wrap=tk.WORD, selectbackground='#D3D3D3') # Light gray selection background
text.pack(expand=True, fill="both")
# Enable Undo/Redo Feature
text.config(undo=True)
# Attach text widget to line numbers
line_numbers.attach(text)
# Initial redraw to display line number 1 before typing
line_numbers.redraw()
# Redraw line numbers on text change
text.bind("<KeyRelease>", lambda event: line_numbers.redraw())
text.bind("<MouseWheel>", lambda event: line_numbers.redraw())
# GUI Main Loop
root.mainloop()