-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notepad.py
37 lines (30 loc) · 1005 Bytes
/
Notepad.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
import tkinter as tk
from tkinter import filedialog
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, "end")
text.insert("1.0", file.read())
def save_file():
file_path = filedialog.asksaveasfilename(filetypes=[("Text Files", "*.txt")])
if file_path:
with open(file_path, "w") as file:
file.write(text.get("1.0", "end"))
# Create the main window
root = tk.Tk()
root.title("Python Notepad")
# Create a text widget
text = tk.Text(root, wrap=tk.WORD)
text.pack()
# Create a menu
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# Run the application
root.mainloop()