Make A Notepad Using Tkinter Module In Python

by | Mar 19, 2022 | Coding, Python

Home » DIY » Make A Notepad Using Tkinter Module In Python

Introduction of the Project

Do you use Notepad? Ever tried to make one? In today’s python tutorial, we will make a notepad using the Tkinter module. Coding is fun, and here, you will be guided step by step for all the instructions required to make Notepad. So let’s start our journey to make Notepad.



1. To run the code, you need Python; you can use VSCode or any python IDE.

2. Pre-installed Tkinter module to perform GUI tasks.

Steps To Make A Notepad Using Tkinter Module In Python

Step 1: Installing the Tkinter module

Windows: Open Command Prompt and type

macOS: Open Terminal and type

pip3 install tk

Step 2: Copy the below piece of code in your editor/IDE.

Source Code

# Import modules
import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
# A class to design overall layout and function in notepad
class Notepad:
    root = Tk()
    # Here, we are assigning default window's width and height and widgets
    thisWidth = 600
    thisHeight = 400
    thisTextArea = Text(root)
    thisMenuBar = Menu(root)
    thisFileMenu = Menu(thisMenuBar, tearoff=0)
    thisEditMenu = Menu(thisMenuBar, tearoff=0)
    thisHelpMenu = Menu(thisMenuBar, tearoff=0)
    # To add scrollbar in notepad
    thisScrollBar = Scrollbar(thisTextArea)	
    file = None
    def __init__(self,**kwargs):
        # To set icon
        # Set the title of this widget
        self.root.title("Untitled - Notepad")
        # To return the number of pixels of the width of the notepad screen in pixel
        screenWidth = self.root.winfo_screenwidth()
        # To return the number of pixels of the height of the notepad screen in pixel
        screenHeight = self.root.winfo_screenheight()
        # For left alignment
        left = (screenWidth / 2) - (self.thisWidth / 2)
        # For right alignment
        top = (screenHeight / 2) - (self.thisHeight /2)
        # Set geometry of the form = widthxheight+x+y
        self.root.geometry('%dx%d+%d+%d' % (self.thisWidth, self.thisHeight, left, top))
        # To auto-resize the textarea
        self.root.grid_rowconfigure(0, weight=1)
        self.root.grid_columnconfigure(0, weight=1)
        # To position a widget in the parent widget in a grid
        self.thisTextArea.grid(sticky = N + E + S + W)
        # To open a new file
        self.thisFileMenu.add_command(label="New", command=self.__newFile)
        # To open an already existing file
        self.thisFileMenu.add_command(label="Open", command=self.__openFile)
        # To save the current file
        self.thisFileMenu.add_command(label="Save", command=self.__saveFile)
        # To exit the window	
        self.thisFileMenu.add_command(label="Exit", command=self.__exitApplication)
        # Add hierarchical menu item, here under File, items are created
        self.thisMenuBar.add_cascade(label="File", menu=self.thisFileMenu)	
        # To create an option of cut
        self.thisEditMenu.add_command(label="Cut", command=self.__cut)			
        # To create an option of copy
        self.thisEditMenu.add_command(label="Copy", command=self.__copy)		
        # To create an option of paste
        self.thisEditMenu.add_command(label="Paste", command=self.__paste)		
        # To create an option for Edit
        self.thisMenuBar.add_cascade(label="Edit", menu=self.thisEditMenu)	
        # To create an option to know about Notepad
        self.thisHelpMenu.add_command(label="About Notepad", command=self.__showAbout)
        self.thisMenuBar.add_cascade(label="Help", menu=self.thisHelpMenu)
        # To configure MenuBar & scrollbar of notepad
        # To adjust th scrollbar automatically
    # To exit application	
    def __exitApplication(self):
    # To show about message
    def __showAbout(self):
        showinfo("Notepad","This is a Notepad Using Tkinter Module In Python")
    # A method to open the file
    def __openFile(self):
        # Ask for a filename to open
        self.__file = askopenfilename(defaultextension=".txt", filetypes=[("All Files","*.*"), ("Text Documents","*.txt")])
        # To select the final component of a pathname
        if self.__file == "":
            self.__file = None
            self.root.title(os.path.basename(self.__file) + " - Notepad")
            file = open(self.__file,"r")
    # To set the title of the new file	
    def __newFile(self):
        self.root.title("Untitled - Notepad")
        self.__file = None
    # To save the file
    def __saveFile(self):
        if self.__file == None:
            # Ask for a filename to save as
            self.__file = asksaveasfilename(initialfile='Untitled.txt', defaultextension=".txt", filetypes=[("All Files","*.*"), ("Text Documents","*.txt")])
            if self.__file == "":
                self.__file = None
                # To save the file
                file = open(self.__file,"w")
                # To change the window title
                self.root.title(os.path.basename(self.__file) + " - Notepad")
            file = open(self.__file,"w")
    # A method for cut functionality
    def __cut(self):
    # A method for copy functionality
    def __copy(self):
    # A method for paste functionality
    def __paste(self):
    # To run the application
    def run(self):
notepad = Notepad(width=600,height=400)

Explanation Of The Code

Firstly we imported the OS and Tkinter modules.

1. Inside the class notepad, we have defined the overall layout along with its features.

2. Then, we have assigned the default window’s width and height. We have also added a scrollbar using the scrollbar function.

3. Now, we have set the icon using BITMAP and the title of the Notepad window using the title function.

4. Set geometry of the form = widthxheight+x+y

5. Then, at the top of the notepad, we have added the items using add command function.

6. Then, we have created a method for each of the menu options.

7. Finally, we are running the file using the run function.


The GUI of Notepad coded by us will look like the below image.

Make A Notepad Using Tkinter Module In Python


We have successfully run the code to make a notepad using the Tkinter module in python and created a Notepad. Before pasting the code, ensure that all the prerequisites have been met for smooth execution.


You May Also Like To Create…