-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.py
More file actions
114 lines (95 loc) · 3.55 KB
/
notepad.py
File metadata and controls
114 lines (95 loc) · 3.55 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
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext
# Initialize the main window
root = tk.Tk()
root.title("Notepad")
root.geometry("800x600")
# Global variable to hold the current file path
current_file = None
# Function to open a file
def open_file():
global current_file
current_file = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"),
("All Files", "*.*")])
if current_file:
root.title(f"Notepad - {current_file}")
with open(current_file, "r") as file:
text_area.delete(1.0, tk.END)
text_area.insert(tk.END, file.read())
# Function to save the current file
def save_file():
global current_file
if current_file:
with open(current_file, "w") as file:
file.write(text_area.get(1.0, tk.END))
messagebox.showinfo("Notepad", "File Saved")
else:
save_as_file()
# Function to save the current file with a new name
def save_as_file():
global current_file
current_file = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"),
("All Files", "*.*")])
if current_file:
with open(current_file, "w") as file:
file.write(text_area.get(1.0, tk.END))
root.title(f"Notepad - {current_file}")
messagebox.showinfo("Notepad", "File Saved")
# Function to create a new file
def new_file():
global current_file
current_file = None
text_area.delete(1.0, tk.END)
root.title("Notepad")
# Function to exit the application
def exit_app():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
# Function to cut text
def cut_text():
text_area.event_generate("<<Cut>>")
# Function to copy text
def copy_text():
text_area.event_generate("<<Copy>>")
# Function to paste text
def paste_text():
text_area.event_generate("<<Paste>>")
# Function to undo text
def undo_text():
text_area.event_generate("<<Undo>>")
# Function to redo text
def redo_text():
text_area.event_generate("<<Redo>>")
# Create a Menu bar
menu_bar = tk.Menu(root)
# Create File menu
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open...", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_command(label="Save As...", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_app)
menu_bar.add_cascade(label="File", menu=file_menu)
# Create Edit menu
edit_menu = tk.Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Undo", command=undo_text)
edit_menu.add_command(label="Redo", command=redo_text)
edit_menu.add_separator()
edit_menu.add_command(label="Cut", command=cut_text)
edit_menu.add_command(label="Copy", command=copy_text)
edit_menu.add_command(label="Paste", command=paste_text)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
# Add Help menu
help_menu = tk.Menu(menu_bar, tearoff=0)
help_menu.add_command(label="About", command=lambda: messagebox.showinfo("Notepad", "Tkinter Notepad by OpenAI's ChatGPT"))
menu_bar.add_cascade(label="Help", menu=help_menu)
# Configure the menu
root.config(menu=menu_bar)
# Create the main text area
text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD, undo=True)
text_area.pack(fill=tk.BOTH, expand=True)
# Run the application
root.mainloop()