Make a text editing application using python.
If you want to make a text editor application using python, then you are at the right place. Just follow these step and copy paste all the codes given below (same name and extensions as given): Copy this Python code and save it as "text-editor.py" in your device.
Python:
import tkinter as tk
from tkinter import filedialog
def open_file():
filepath = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
if filepath:
with open(filepath, "r") as file:
editor.delete("1.0", tk.END)
editor.insert("1.0", file.read())
def save_file():
filepath = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
if filepath:
with open(filepath, "w") as file:
file.write(editor.get("1.0", tk.END))
root = tk.Tk()
root.title("Text Editor")
editor = tk.Text(root, wrap="word")
editor.pack(expand=True, fill="both")
menu_bar = tk.Menu(root)
file_menu = tk.Menu(menu_bar, tearoff=0)
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)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)
root.mainloop()
0 Comments