Notepad is a simple and widely-used text editor, and with the help of the Tkinter module in Python, we can build our own version. Tkinter provides an easy-to-use interface for creating graphical user interfaces (GUIs).
The provided code is an implementation of a basic text editor using the Tkinter library in Python. Let’s go through the code step by step and explain its functionality:
Importing Required Modules:
import os.path
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfilename, asksaveasfilename
- os.path: This module provides functions for working with file paths.
- tkinter: It is a standard Python interface to the Tk GUI toolkit.
- tkinter.messagebox: It provides the showinfo function to display message boxes.
- tkinter.filedialog: It provides functions to open and save files using a dialog box.
Creating the Application Window:
root = Tk()
root.title("untitled~notepad")
root.geometry("1140x930")
-
Tk(): Creates the main application window.
- title(): Sets the title of the window.
- geometry(): Sets the size of the window.
Creating the Text Area:
TextArea = Text(font="lucida 13")
TextArea.pack(expand=True, fill=BOTH)
-
Text(): Creates a text widget where the user can input and view text.
- pack(): Organizes the widget in the window.
Global Variables and Functions:
def new_file():
global file
root.title("untitled~notepad")
file = None
TextArea.delete(1.0, END)
def open_file():
global file
file = askopenfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) + " - Notepad")
TextArea.delete(1.0, END)
with open(file, "r") as f:
TextArea.insert(1.0, f.read())
def save_file():
global file
if file is None:
file = asksaveasfilename(initialfile="untitled.txt", defaultextension=".txt",
filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
with open(file, "w") as f:
f.write(TextArea.get(1.0, END))
root.title(os.path.basename(file) + " - Notepad")
print("File saved successfully")
else:
with open(file, "w") as f:
f.write(TextArea.get(1.0, END))
def cut_text():
TextArea.event_generate("<<Cut>>")
def copy_text():
TextArea.event_generate("<<Copy>>")
def paste_text():
TextArea.event_generate("<<Paste>>")
def about_notepad():
showinfo("About Notepad", "Made with Love by Developer Ritesh")
def quit_notepad():
root.destroy()
These functions will be used to perform various operations on the text editor, such as creating a new file, opening an existing file, saving the file, etc.
Creating the Menus:
menubar = Menu(root)
file_menu = Menu(menubar, tearoff=0)
# File menu options
edit_menu = Menu(menubar, tearoff=0)
# Edit menu options
help_menu = Menu(menubar, tearoff=0)
# Help menu options
root.config(menu=menubar)
-
Menu(): Creates a menu widget.
- menubar: The main menu bar for the window.
- tearoff=0: Disables the tear-off feature of menus.
File Menu Options:
file_menu.add_command(label="New", command=new_file)
file_menu.add_separator()
file_menu.add_command(label="Open", command=open_file)
file_menu.add_separator()
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit_notepad)
menubar.add_cascade(label="File", menu=file_menu)
-
add_command(): Adds a menu item with a label and a command.
- add_separator(): Adds a separator line between menu items.
- add_cascade(): Adds a menu cascade, which is a hierarchical menu.
Edit Menu Options:
edit_menu.add_command(label="Cut", command=cut_text)
edit_menu.add_separator()
edit_menu.add_command(label="Copy", command=copy_text)
edit_menu.add_separator()
edit_menu.add_command(label="Paste", command=paste_text)
menubar.add_cascade(label="Edit", menu=edit_menu)
The edit menu options include “Cut,” “Copy,” and “Paste.” They are associated with their respective functions defined earlier.
Help Menu Options:
help_menu.add_command(label="About Notepad", command=about_notepad)
menubar.add_cascade(label="Help", menu=help_menu)
The help menu includes an “About Notepad” option, which is associated with the about_notepad() function.
Scrollbar:
scroll = Scrollbar(TextArea)
scroll.pack(side=RIGHT, fill="y")
scroll.config(command=TextArea.yview)
TextArea.config(yscrollcommand=scroll.set)
A scrollbar is added to the text area to scroll through the content.
Configuring the Application:
root.config(menu=menubar)
The application’s main window is configured to display the menu bar.
Running the Application:
root.mainloop()
-
The main event loop that handles user events and keeps the application running.
Full Source Code
import os.path
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfilename, asksaveasfilename
root = Tk()
root.title("untitled~notepad")
root.geometry("1140x930")
TextArea = Text(font="lucida 13")
file = None
TextArea.pack(expand=True, fill=BOTH)
def new_file():
global file
root.title("untitled~notepad")
file = None
TextArea.delete(1.0, END)
def open_file():
global file
file = askopenfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) + " - Notepad")
TextArea.delete(1.0, END)
with open(file, "r") as f:
TextArea.insert(1.0, f.read())
def save_file():
global file
if file is None:
file = asksaveasfilename(initialfile="untitled.txt", defaultextension=".txt",
filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
with open(file, "w") as f:
f.write(TextArea.get(1.0, END))
root.title(os.path.basename(file) + " - Notepad")
print("File saved successfully")
else:
with open(file, "w") as f:
f.write(TextArea.get(1.0, END))
def cut_text():
TextArea.event_generate("<<Cut>>")
def copy_text():
TextArea.event_generate("<<Copy>>")
def paste_text():
TextArea.event_generate("<<Paste>>")
def about_notepad():
showinfo("About Notepad", "Made with Love by Developer Ritesh")
def quit_notepad():
root.destroy()
menubar = Menu(root)
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_separator()
file_menu.add_command(label="Open", command=open_file)
file_menu.add_separator()
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit_notepad)
menubar.add_cascade(label="File", menu=file_menu)
edit_menu = Menu(menubar, tearoff=0)
edit_menu.add_command(label="Cut", command=cut_text)
edit_menu.add_separator()
edit_menu.add_command(label="Copy", command=copy_text)
edit_menu.add_separator()
edit_menu.add_command(label="Paste", command=paste_text)
menubar.add_cascade(label="Edit", menu=edit_menu)
help_menu = Menu(menubar, tearoff=0)
help_menu.add_command(label="About Notepad", command=about_notepad)
menubar.add_cascade(label="Help", menu=help_menu)
scroll = Scrollbar(TextArea)
scroll.pack(side=RIGHT, fill="y")
scroll.config(command=TextArea.yview)
TextArea.config(yscrollcommand=scroll.set)
TextArea.pack()
root.config(menu=menubar)
root.mainloop()