-
Notifications
You must be signed in to change notification settings - Fork 0
/
textEditor.py
80 lines (68 loc) · 3.24 KB
/
textEditor.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
import tkinter as tk
from tkinter import filedialog
class TextEditor:
def __init__(self, root):
self.root = root
self.root.title("Text Editor")
self.text = tk.Text(root, wrap="word", undo=True, bg='#212529', fg='#d9ed92', insertbackground='#a2d2ff')
self.text.pack(expand="yes", fill="both")
self.menu_bar = tk.Menu(root)
self.root.config(menu=self.menu_bar)
# File menu
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
self.file_menu.add_command(label="New", command=self.new_file)
self.file_menu.add_command(label="Open", command=self.open_file)
self.file_menu.add_command(label="Save", command=self.save_file)
self.file_menu.add_command(label="Save As", command=self.save_as_file)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=root.destroy)
# Edit menu
self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu)
self.edit_menu.add_command(label="Undo", command=self.text.edit_undo)
self.edit_menu.add_command(label="Redo", command=self.text.edit_redo)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="Cut", command=self.cut_text)
self.edit_menu.add_command(label="Copy", command=self.copy_text)
self.edit_menu.add_command(label="Paste", command=self.paste_text)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="Select All", command=self.select_all)
def new_file(self):
self.text.delete(1.0, tk.END)
def open_file(self):
file_path = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, "r") as file:
content = file.read()
self.text.delete(1.0, tk.END)
self.text.insert(tk.END, content)
self.root.title(f"Text Editor - {file_path}")
def save_file(self):
if hasattr(self, "current_file"):
content = self.text.get(1.0, tk.END)
with open(self.current_file, "w") as file:
file.write(content)
else:
self.save_as_file()
def save_as_file(self):
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
content = self.text.get(1.0, tk.END)
with open(file_path, "w") as file:
file.write(content)
self.current_file = file_path
self.root.title(f"Text Editor - {file_path}")
def cut_text(self):
self.text.event_generate("<<Cut>>")
def copy_text(self):
self.text.event_generate("<<Copy>>")
def paste_text(self):
self.text.event_generate("<<Paste>>")
def select_all(self):
self.text.tag_add(tk.SEL, "1.0", tk.END)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("600x400") # Set initial window size
app = TextEditor(root)
root.mainloop()