Develop Notepad using Tkinter in python

Tkinter is a standard GUI library in Python that enables developers to create desktop applications. In this tutorial, we'll build a fully functional notepad text editor with file operations, editing features, and menu functionality using Tkinter.

Prerequisites

  • Python installed (3.x recommended)
  • Tkinter module (comes pre-installed with Python)

Note: Tkinter is included as a standard library with Python 3.x installations.

Notepad Menu Structure

Our notepad will contain four main menu categories with their respective sub-items:

  • File Menu: New, Open, Save, Exit
  • Edit Menu: Cut, Copy, Paste
  • Commands Menu: About Commands
  • Help Menu: About Notepad

Creating Menu Items

Here's how we add menu items and their functionality ?

# File menu commands
self.__thisFileMenu.add_command(label="New", command=self.__newFile)
self.__thisFileMenu.add_command(label="Open", command=self.__openFile)
self.__thisFileMenu.add_command(label="Save", command=self.__saveFile)
self.__thisFileMenu.add_separator()
self.__thisFileMenu.add_command(label="Exit", command=self.__quitApplication)
self.__thisMenuBar.add_cascade(label="File", menu=self.__thisFileMenu)

# Edit menu commands
self.__thisEditMenu.add_command(label="Cut", command=self.__cut)
self.__thisEditMenu.add_command(label="Copy", command=self.__copy)
self.__thisEditMenu.add_command(label="Paste", command=self.__paste)
self.__thisMenuBar.add_cascade(label="Edit", menu=self.__thisEditMenu)

# Help and Commands menus
self.__thisHelpMenu.add_command(label="About Notepad", command=self.__showAbout)
self.__thisCommandMenu.add_command(label="About Commands", command=self.__showCommand)
self.__thisMenuBar.add_cascade(label="Commands", menu=self.__thisCommandMenu)
self.__thisMenuBar.add_cascade(label="Help", menu=self.__thisHelpMenu)

Implementing Menu Functions

Each menu item requires a corresponding function to handle its operation ?

def __quitApplication(self):
    self.__root.destroy()

def __showAbout(self):
    showinfo("About Notepad", "Simple text editor like notepad using Python")

def __showCommand(self):
    showinfo("Notepad", "Just Another TextPad \n Copyright \n with BSD license")

def __openFile(self):
    self.__file = askopenfilename(defaultextension=".txt", 
                                  filetypes=[("All Files","*.*"),
                                           ("Text Documents","*.txt")])
    if self.__file == "":
        self.__file = None
    else:
        self.__root.title(os.path.basename(self.__file) + " - Notepad")
        self.__thisTextArea.delete(1.0, END)
        with open(self.__file, "r") as file:
            self.__thisTextArea.insert(1.0, file.read())

def __newFile(self):
    self.__root.title("Untitled - Notepad")
    self.__file = None
    self.__thisTextArea.delete(1.0, END)

def __saveFile(self):
    if self.__file == None:
        self.__file = asksaveasfilename(initialfile='Untitled.txt', 
                                        defaultextension=".txt", 
                                        filetypes=[("All Files","*.*"), 
                                                 ("Text Documents","*.txt")])
    if self.__file != "":
        with open(self.__file, "w") as file:
            file.write(self.__thisTextArea.get(1.0, END))
        self.__root.title(os.path.basename(self.__file) + " - Notepad")

def __cut(self):
    self.__thisTextArea.event_generate("<<Cut>>")

def __copy(self):
    self.__thisTextArea.event_generate("<<Copy>>")

def __paste(self):
    self.__thisTextArea.event_generate("<<Paste>>")

Complete Notepad Application

Here's the complete code for our Tkinter notepad application ?

import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *

class Notepad:
    # Initialize window components
    __root = Tk()
    __thisWidth = 500
    __thisHeight = 700
    __thisTextArea = Text(__root)
    __thisMenuBar = Menu(__root)
    __thisFileMenu = Menu(__thisMenuBar, tearoff=0)
    __thisEditMenu = Menu(__thisMenuBar, tearoff=0)
    __thisHelpMenu = Menu(__thisMenuBar, tearoff=0)
    __thisCommandMenu = Menu(__thisMenuBar, tearoff=0)
    __thisScrollBar = Scrollbar(__thisTextArea)
    __file = None

    def __init__(self, **kwargs):
        # Set window icon (optional)
        try:
            self.__root.wm_iconbitmap("Notepad.ico")
        except:
            pass

        # Set window dimensions
        try:
            self.__thisWidth = kwargs['width']
        except KeyError:
            pass

        try:
            self.__thisHeight = kwargs['height']
        except KeyError:
            pass

        # Set window title and center it
        self.__root.title("Untitled - Notepad")
        screenWidth = self.__root.winfo_screenwidth()
        screenHeight = self.__root.winfo_screenheight()
        left = (screenWidth / 2) - (self.__thisWidth / 2)
        top = (screenHeight / 2) - (self.__thisHeight / 2)
        
        self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth, self.__thisHeight, left, top))
        
        # Make textarea resizable
        self.__root.grid_rowconfigure(0, weight=1)
        self.__root.grid_columnconfigure(0, weight=1)
        self.__thisTextArea.grid(sticky=N + E + S + W)

        # Create File menu
        self.__thisFileMenu.add_command(label="New", command=self.__newFile)
        self.__thisFileMenu.add_command(label="Open", command=self.__openFile)
        self.__thisFileMenu.add_command(label="Save", command=self.__saveFile)
        self.__thisFileMenu.add_separator()
        self.__thisFileMenu.add_command(label="Exit", command=self.__quitApplication)
        self.__thisMenuBar.add_cascade(label="File", menu=self.__thisFileMenu)

        # Create Edit menu
        self.__thisEditMenu.add_command(label="Cut", command=self.__cut)
        self.__thisEditMenu.add_command(label="Copy", command=self.__copy)
        self.__thisEditMenu.add_command(label="Paste", command=self.__paste)
        self.__thisMenuBar.add_cascade(label="Edit", menu=self.__thisEditMenu)

        # Create Help and Commands menus
        self.__thisHelpMenu.add_command(label="About Notepad", command=self.__showAbout)
        self.__thisCommandMenu.add_command(label="About Commands", command=self.__showCommand)
        self.__thisMenuBar.add_cascade(label="Commands", menu=self.__thisCommandMenu)
        self.__thisMenuBar.add_cascade(label="Help", menu=self.__thisHelpMenu)

        # Configure menu bar and scrollbar
        self.__root.config(menu=self.__thisMenuBar)
        self.__thisScrollBar.pack(side=RIGHT, fill=Y)
        self.__thisScrollBar.config(command=self.__thisTextArea.yview)
        self.__thisTextArea.config(yscrollcommand=self.__thisScrollBar.set)

    def __quitApplication(self):
        self.__root.destroy()

    def __showAbout(self):
        showinfo("About Notepad", "Simple text editor created using Python Tkinter")

    def __showCommand(self):
        showinfo("Commands", "File: New, Open, Save, Exit\nEdit: Cut, Copy, Paste")

    def __openFile(self):
        self.__file = askopenfilename(defaultextension=".txt", 
                                      filetypes=[("All Files","*.*"),
                                               ("Text Documents","*.txt")])
        if self.__file == "":
            self.__file = None
        else:
            self.__root.title(os.path.basename(self.__file) + " - Notepad")
            self.__thisTextArea.delete(1.0, END)
            with open(self.__file, "r") as file:
                self.__thisTextArea.insert(1.0, file.read())

    def __newFile(self):
        self.__root.title("Untitled - Notepad")
        self.__file = None
        self.__thisTextArea.delete(1.0, END)

    def __saveFile(self):
        if self.__file == None:
            self.__file = asksaveasfilename(initialfile='Untitled.txt', 
                                            defaultextension=".txt", 
                                            filetypes=[("All Files","*.*"), 
                                                     ("Text Documents","*.txt")])
        if self.__file != "":
            with open(self.__file, "w") as file:
                file.write(self.__thisTextArea.get(1.0, END))
            self.__root.title(os.path.basename(self.__file) + " - Notepad")

    def __cut(self):
        self.__thisTextArea.event_generate("<<Cut>>")

    def __copy(self):
        self.__thisTextArea.event_generate("<<Copy>>")

    def __paste(self):
        self.__thisTextArea.event_generate("<<Paste>>")

    def run(self):
        self.__root.mainloop()

# Create and run the notepad application
if __name__ == "__main__":
    notepad = Notepad(width=600, height=400)
    notepad.run()

Features of the Notepad

  • File Operations: Create new files, open existing files, and save documents
  • Text Editing: Cut, copy, and paste functionality using keyboard events
  • Scrollbar: Automatic scrolling for large documents
  • Resizable Window: Dynamic text area that adjusts to window size
  • File Dialogs: Standard open and save file dialogs

Conclusion

This Tkinter notepad provides essential text editing functionality with a clean, user-friendly interface. The application demonstrates key GUI concepts including menus, file dialogs, and event handling. You can extend this foundation by adding features like find/replace, font selection, or syntax highlighting.

Updated on: 2026-03-25T05:22:30+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements