Sitemap

Simple Notepad using Python

3 min readMay 28, 2023

--

Python allows the development of desktop applications using frameworks like PyQt and Tkinter. These frameworks provide tools for creating GUI-based applications for tasks like image editing, text processing, or project management. Its applications in daily life are numerous and continue to expand as Python evolves and adapts to new challenges. Python can be helpful in many ways in our daily life, and one of the ways is to use applications daily. Here, We will learn how to create a simple Notepad app in Python.

So let's start with the simple code and then a detailed explanation of how it works.

from tkinter import *
from tkinter import filedialog
def new_file():
text.delete("1.0", END)
def open_file():
file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, "r") as file:
text.delete("1.0", END)
text.insert(END, file.read())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, "w") as file:
file.write(text.get("1.0", END))
root = Tk()
root.title("Notepad")
menu = Menu(root)
root.config(menu=menu)
file_menu = Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
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_separator()
file_menu.add_command(label="Exit", command=root.quit)
text = Text(root)
text.pack()
root.mainloop()

Save the code in a Python file (e.g., notepad.py) and run it. The application will open a simple Notepad window where you can create, open, and save text files.

Get Neha Saini’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s go through the code step by step and explain its functionality in plain English:

  1. The code begins by importing necessary modules from the tkinter library. Tk is the main module for creating the application window, and filedialog provides functionality for opening and saving files.
  2. The new_file the function is defined to handle the "New" menu command. It clears the text area by deleting all content.
  3. The open_file the function is defined to handle the "Open" menu command. It opens a file dialog box where the user can select a text file to open. If a file is selected, its contents are read and displayed in the text area.
  4. The save_file the function is defined to handle the "Save" menu command. It opens a file dialog box where the user can specify the location and name of the file to be saved. If a file name is provided, the contents of the text area are written to the file.
  5. An instance of the Tk class is created, which represents the main application window. The window's title is set to "Notepad".
  6. A menu bar is created and associated with the application window using the config method.
  7. A “File” menu is created and added to the menu bar using the add_cascade method. This menu will contain options like "New", "Open", "Save", and "Exit".
  8. Each menu option is defined using the add_command method. The label represents the text displayed for the option, and the associated command specifies the function to be executed when the option is selected.
  9. A separator is added using the add_separator method to visually separate the "Save" option from the "Exit" option.
  10. The Text widget is created and packed into the application window. This widget provides an area where the user can type and view text.
  11. Finally, the mainloop the function is called on the application window, which starts the event handling loop and allows the application to respond to user interactions.

When you run the code, it will open a simple notepad-like application window. You can use the “New” option to clear the text area, “Open” to select and display the contents of a text file, and “Save” to save the current text in the text area to a file. The “Exit” option can be used to close the application.

--

--

Neha Saini
Neha Saini

Written by Neha Saini⁠

A Software Programmer with more than a decade of experience in the industry, passion for writing and curious mind .