
Introduction:
In this project, we will show how to use the Python to build a Notepad. This Notepad uses a graphical user interface(GUI) to save and open the text file. This notepad is build using the Tkinter library, which provides a fast and easy way to create GUI applications. This application has an easy and simple design with editor and menu. Grab a cup of coffee, sit down and start Notepad using python Application.
Explanation:
This Notepad application is a Graphical user interface(GUI) application opens and saves text file using menu component of Tkinter library. It defines functions for opening, saving, and exiting the editor, as well as an “About” dialog box. The functions open_file and save_file use filedialog to interact with the user’s file system for opening and saving files. The about function displays an information dialog. The exit_editor function prompts the user if they really want to exit before closing the application. Tkinter is a Python package that makes it simple to construct graphical user interfaces.
Source Code:
import tkinter as tk
from tkinter import filedialog, messagebox
def open_file():
filepath = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if filepath:
with open(filepath, 'r') as file:
editor.delete(1.0, tk.END)
editor.insert(tk.END, file.read())
def save_file():
filepath = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if filepath:
with open(filepath, 'w') as file:
file.write(editor.get("1.0",'end-1c'))
def about():
messagebox.showinfo("About", "Simple Text Editor\nCreated by Your Name")
def exit_editor():
if messagebox.askyesno("Exit", "Do you really want to exit?"):
root.destroy()
root = tk.Tk()
root.title("Simple Text Editor")
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=exit_editor)
menu_bar.add_cascade(label="File", menu=file_menu)
help_menu = tk.Menu(menu_bar, tearoff=0)
help_menu.add_command(label="About", command=about)
menu_bar.add_cascade(label="Help", menu=help_menu)
root.config(menu=menu_bar)
editor = tk.Text(root, wrap="word")
editor.pack(expand="yes", fill="both")
root.mainloop()
Output :
