Create a Text Editor in Python
In this tutorial, we will learn how to create a text editor like notepad in Python.
Text Editor is the first most and essential need of a software developer those are willing to write code or simply edit the existing one, working becomes easier due to the different functionality provided to the user, they are customizable according to the user needs. As though the ultimate work of a text editor is to edit files then too, choosing the best editor for programming is a trending topic of all-time in the tech industry, depending upon you can predict the power and importance of text editor in developer’s life.
So in this tutorial, we will be learning how to create a simple text editor using Python Programming Language. In this, we will be using the Tkinter library and two additional modules supported by Tkinter, ‘MessageBox’ and ‘FileDialog’ for providing some additional functionality to our editor.
Introduction to the libraries and modules used
-
– This is one of the most Powerful, Widely Used and Platform Independent library available in Python for creating seamless and nice-looking GUI applications, it is very easy to use and understand different widgets provided by Tkinter. – This module provides the template classes and a variety of inbuilt functions for alerting the user, these message boxes can be an info message, an error message or ask yes/no message.
- FileDialog – This module provides the classes and inbuilt functions for creating file or directory selection windows.
Creating Text Editor Using Python
Before getting started with code we need to install required libraries:
Installation:
Now, comes our Actual Code:
Source Code:
I would suggest you, read the code thoroughly because it is very easy to understand and I have added comments for almost every single line for your better understanding.
Output:
Run the text editor file:
Click on the file menu and select open command:

Select the file you want to open:

Click on open Button:

Now you can enjoy editing your file:

So In this way, you can create a simple text editor using Python and Tkinter library. I hope this tutorial was helpful to you, thank you ‘Keep Learning Keep Coding’.
10 responses to “Create a Text Editor in Python”
Hey, I love the tutorial but you used read mode in askopenfilename that is only meant to read not edit or write to the file content.
HI. How can i change the color of the background window??
I try this:
And is not working.
I like it a lot but if I spell a word wrong, it doesn’t correct me…or even put a red line under it to tell me that it is spelled wrong
Very cool. I used this for a text compression project.
I did make some minor changes, as I needed it to compress the text, and it didn’t handle UTF-8 text well.
The final project is on my GitHub. codeBodger/Text-Compression
very interesting Text editor. how to add a small dictionary to correct a spelling error?
i want add kannada word writing on this text editing , how to do this?
bro,
what does (self.) after every line means here??
Excuse me, mister Sameer, But would you mind if I copied your code, gave you explicit credit, modified the fonts a little bit, and made this into an opensource text editor project? I would really like it if we made an opensource project and it became famous because we very kewl. I thinking about adding a customizable gui and a package manager for code libraries, used for syntax checking.
Name already in use
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Простой пример текстового редактора при помощи tkinter Python3. Полностью на русском языке.
Simple example text editor by tkinter Python 3. On Russian language.
Пример программы по окнончанию курса
About
Полное руководство по созданию текстового редактора, от азов до ООП, при помощи tkinter Python3. Полностью на русском языке. Simple example text editor by tkinter Python 3. On Russian language.
Make a simple text editor using Python
Today we’ll be making a simple text editor using Python and Tkinter.
What is tkinter?
- It is a standard Python interface to the Tk GUI toolkit shipped with Python.
How do I know if I have installed tkinter?
- Most probably, you would have it installed. Try running python -m tkinter in your console. If it doesn’t give an error, it means you have tkinter installed and ready to use!
I don’t have tkinter installed, what should I do?
- Just make sure you have Python and pip working, then run pip install tk in the console.
Prerequisites
- Python 3.9.x
- Code editor (VSCode, PyCharm, etc)
Now that that’s out of the way, let’s get started.
The tutorial starts here.
First, we’ll make a file structure sort of thing.
We’ll need these files (they can be in the root directory):
- main.py
- file_menu.py
- edit_menu.py
- format_menu.py
- help_menu.py
main.py
For the main.py file, we’ll start by importing the tkinter library and the menu files we made just now:
Exit fullscreen mode
Then, we’ll make a tkinter window like this:
Exit fullscreen mode
Then, we’ll give our text editor some dimensions and a title like this:
Exit fullscreen mode
Then, we’ll add this code:
Exit fullscreen mode
Then, we’ll make a menubar :
Exit fullscreen mode
Then, we’ll add our menus to the menubar:
Exit fullscreen mode
Finally, we’ll add this code so this editor actually functions:
Exit fullscreen mode
Let’s move on to our menu files, shall we?
file_menu.py
First, we’ll import stuff:
Exit fullscreen mode
Then, we’ll make a class File :
Exit fullscreen mode
Then we’ll add all the functions we need to our File class:
Exit fullscreen mode
In the above code, we’ve added some functions:
- New File
- Save File
- Save File As
- Open File
- Quit Editor
Then, we’ll make a main function (note that this function should not be under the File class):
Exit fullscreen mode
In the above code, we made a main function which basically executes all functions under the File class.
Finally, we add some error-handling sort of thingy:
Exit fullscreen mode
edit_menu.py
We’ll start by importing stuff:
Exit fullscreen mode
Then, we’ll make a class Edit :
Exit fullscreen mode
Then we’ll add all the necessary functions under this Edit class:
Exit fullscreen mode
In the above code, we make some functions:
- Copy
- Cut
- Paste
- Select All
- Undo
- Redo
- Find
Then, we make a main function outside the Edit class:
Exit fullscreen mode
Finally, we’ll do some error-handling thingy:
Exit fullscreen mode
format_menu.py
We’ll start by importing stuff:
Exit fullscreen mode
Then, we make a class Format :
Exit fullscreen mode
Then, we add the necessary functions:
Exit fullscreen mode
In the above code, we added some functions:
- Change Background
- Change Font Group
- Bold Text
- Italic Text
- Underline Text
- Over Strike Text
- Add Date
Then, we make a main function:
Exit fullscreen mode
Finally, we add some error-handling stuff:
Exit fullscreen mode
help_menu.py
We’ll start by importing stuff
Exit fullscreen mode
Then, we’ll make a Help class and add an About function to it:
Exit fullscreen mode
We then make a main function:
Exit fullscreen mode
Finally, we add some error-handling sort of thing:
Exit fullscreen mode
And that should be it!
Thanks for reading this post till the end. Hopefully it helped you.
If there is anything missing, or I did something wrong, kindly point it out in the comments 😀
Socials
Top comments (1)
![]()
ok quite helpful
For further actions, you may consider blocking this person and/or reporting abuse
Timeless DEV post.
Git Concepts I Wish I Knew Years Ago
The most used technology by developers is not Javascript.
It’s not Python or HTML.
It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.
Text Editor Application Using Tkinter (Python Project)
In this tutorial, we will help you to build a simple Text Editor Application using Tkinter which is a very good beginner project for Tkinter.
Text Editor Application is an application where you can write your text, open any text file, you can edit any text file and you can also save a file if you want. In this tutorial, we will build a Text Editor Application from scratch.
Essential Elements for the Text editor application are as follows:
There is a Button widget called btn_open that is used for opening a file for editing
Second one is a Button widget called btn_save for saving a file
Third, there is a Text widget called txt_edit for creating and editing any text file.
The arrangement of three widgets is done in a way such that the two buttons are on the left-hand side of the window, and the text box is on the right-hand side. The minimum height of the whole window should be 900 pixels and txt_edit should have a minimum width of 900 pixels. And The whole layout should be responsive if the window is resized, then txt_edit is resized as well. The width of the Frame that holds the buttons should not change.
Let us show you with a rough sketch of how this text editor will look like:

The desired layout of the Text Editor Application can be achieved using the .grid() geometry manager. And this layout contains a single row and two columns:
On the left side, there is A narrow column for the buttons
On the right side, there is A wider column for the text box
In order to set the minimum sizes for the window and txt_edit , you just need to set the minsize parameters of the window methods .rowconfigure() and .columnconfigure() to 900. In order to handle the resizing, the weight parameters of these methods will be set to 1.
If you want both the buttons in the same column then you’ll need to create a Frame widget called fr_buttons . According to the above-shown sketch, the two buttons should be stacked vertically inside of this frame, having btn_open on top. This can be done either by .grid() or .pack() geometry manager. For now, you’ll just need to stick with .grid() as it is easier to work with it.
Let us start with the code for building the Application:
1. Creating all the needed widgets
The code snippet that is used is as follows:
Explanation of the above code:
The first command is used to import the tkinter .
Then the next two lines are used to create a new window with the title «Text Editor Application» .
The next two lines of code are used to set the row and column configurations.
Then the lines from 9 to 12 will create the four widgets you’ll need for the text box, the frame, and the open and save buttons.
The above-given line in the code indicates The minsize parameter of .rowconfigure() is set to 900 and weight is set to 1. The first argument is 0 , which is used to set the height of the first row to 900 pixels and makes sure that the height of the row grows proportionally to the height of the window. There’s only one row in the application layout, so these settings are applied to the entire window.
Then take a look at this line in the code :
In the above code the .columnconfigure() to set the width and weight attributes of the column with index 1 to 900 and 1 , respectively. Keep it in mind that, row and column indices are zero-based, so these settings apply only to the second column. By configuring the second column, the text box will expand and contract naturally when the window is resized, while the column containing the buttons will always remain at a fixed width.
2.Creation of Application Layout
The above two lines of code will create a grid with two rows and one column in the fr_buttons frame since both btn_open and btn_save have their master attribute set to fr_buttons . btn_open is put in the first row and btn_save will be in the second row so that btn_open appears above btn_save in the layout, as planned in the above sketch.
The btn_open and btn_save both have their sticky attributes set to «ew» , which forces the buttons to expand horizontally in both directions and in order to fill the entire frame. It makes sure both buttons are of the same size.
You place 5 pixels of padding around each button just by setting the padx and pady parameters to 5. The btn_open has vertical padding. Because it’s on top, the vertical padding offsets the button down from the top of the window a bit and makes sure that there’s a small gap between this and btn_save .
Now the fr_buttons is laid out and ready to go, you can just set up the grid layout for the rest of the window now:
These above two lines of code are used to create a grid with one row and two columns for window . You place fr_buttons in the first column and txt_edit in the second column so that fr_buttons appears to the left of txt_edit in the window layout.
The sticky parameter for fr_buttons will be set to «ns» , which forces the whole frame to expand vertically and fill the entire height of its column. txt_edit is used to fill its entire grid cell because you set its sticky parameter to «nsew» , which forces it to expand in every direction.
Now we have just created the buttons but these do not work until we add functioning to them So let’s start adding the Functioning of Buttons:
1. Function to Open the File
The code snippet for open_file is as follows:
Explanation:
The Lines from 3 to 5 use the askopenfilename dialog from the tkinter.filedialog module to display a file open dialog and store the selected file path to filepath .
Lines 6 and 7 checks to see if the user closes the dialog box or clicks the Cancel button. If so, then filepath will be None , and the function will return without executing any of the code to read the file and set the text of txt_edit .
Line 8 clears the current contents of txt_edit using .delete() .
Lines 9 and 10 are used to open the selected file and .read() its contents before storing the text as a string.
Line number 11 assigns the string text to txt_edit using .insert() .
Line 12 sets the title of the window so that it contains the path of the open file.
2. Function to Save the File
The code snippet for save_file is as follows:
Explanation:
Lines 3 to 6 use the asksaveasfilename dialog box to get the desired save location from the user. The selected file path is stored in the filepath variable.
Lines 7 and 8 checks to see if the user closes the dialog box or clicks the Cancel button. If so, then filepath will be None , and the function will return without executing any of the code to save the text to a file.
Line 9 creates a new file at the selected file path.
Line 10 extracts the text from txt_edit with .get() method and assigns it to the variable text .
Line 11 writes text to the output file.
Line 12 updates the title of the window so that the new file path is displayed in the window title.
Complete Source code is:

As you can see in the output, we have a basic text editor application in which we can write something and then save the text in a new file or use the Open button to open a file in the editor and then edit it.