Introduction: Create a Simple Python Text Editor!
In this Instructable I will be teaching you how to create a simple text editor with Python and the module Tkinter.
To do this tutorial you need some basic knowledge of Python.
First create a python file called texteditor.py or something like that. Then open it with a text editor like Atom or Notepad ++.
Step 1: Making a Window
To make a text editor we need a window. Type in the file:
import sys
v=sys.python_version
if "2.7" in v:
from Tkinter import *
elif "3.3" in v or "3.4" in v:
from tkinter import *
root=Tk("Text Editor")
root.mainloop()
Then if you are on a mac go Cmd+Shift - search and open "terminal" - then type
python /pathto/texteditor.py
and hit enter.
If you are on a windows, search and open Command prompt, type
python /pathto/texteditor.py
and hit enter. You should se a screen that looks like the picture above.
Congrats!
Step 2: Add a Text Widget
Now we need to add something to type in.
Underneath root=Tk() add two lines like this:
text=Text(root)
text.grid()
Then run the file again like in step one. You should get a bigger screen with a text box in it when you click on it. It will look like the picture above.
Your full code will look like this now:
import sys v=sys.version()
if "2.7" in v:
from Tkinter import *
elif "3.3" in v or "3.4" in v:
from tkinter import *
root=Tk("Text Editor")
text=Text(root)
text.grid()
root.mainloop()
Step 3: Saving Your Text
You need to be able to save your text of course, so we will add a button to save.
Under import Tkinter add
import tkFileDialog
and under import tkinter add
import tkinter.tkFileDialog
Under the last line you added write this:
def saveas(): global text
t = text.get("1.0", "end-1c")
savelocation=tkFileDialog.asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
button=Button(root, text="Save", command=saveas)
button.grid()
Clicking on the button will save your file.
The full code:
import sys v=sys.version()
if "2.7" in v:
from Tkinter import * import tkFileDialog
elif "3.3" in v or "3.4" in v:
from tkinter import * import tkinter.tkFileDialog
root=Tk("Text Editor") text=Text(root)
text.grid()
def saveas():
global text
t = text.get("1.0", "end-1c")
savelocation=tkFileDialog.asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
button=Button(root, text="Save", command=saveas)
button.grid()
root.mainloop()
Step 4: Font Changer
Under the last line you added, add this:
def FontHelvetica():
global text
text.config(font="Helvetica")
def FontCourier():
global text
text.config(font="Courier")
font=Menubutton(root, text="Font")
font.grid()
font.menu=Menu(font, tearoff=0)
font["menu"]=font.menu
helvetica=IntVar()
courier=IntVar()
font.menu.add_checkbutton(label="Courier", variable=courier,
command=FontCourier)
font.menu.add_checkbutton(label="Helvetica", variable=helvetica,
command=FontHelvetica)
Congratulations! You have finished a very simple text editor. If you want more tutorials like this tell me what here.
Run the file to use it!
Full Code:
import sys
v=sys.version()
if "2.7" in v:
from Tkinter import *
import tkFileDialog
elif "3.3" in v or "3.4" in v:
from tkinter import *
import tkinter.tkFileDialog
root=Tk("Text Editor")
text=Text(root)
text.grid()
def saveas():
global text
t = text.get("1.0", "end-1c")
savelocation=tkFileDialog.asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
button=Button(root, text="Save", command=saveas)
button.grid()
def FontHelvetica():
global text
text.config(font="Helvetica")
def FontCourier():
global text
text.config(font="Courier")
font=Menubutton(root, text="Font")
font.grid()
font.menu=Menu(font, tearoff=0)
font["menu"]=font.menu
Helvetica=IntVar()
arial=IntVar()
times=IntVar()
Courier=IntVar()
font.menu.add_checkbutton(label="Courier", variable=Courier,
command=FontCourier)
font.menu.add_checkbutton(label="Helvetica", variable=helvetica,
command=FontHelvetica)
root.mainloop()
27 Comments
1 year ago
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import tkFileDialog
except ImportError:
import tkinter.filedialog as tkFileDialog
###v=sys.version()
'''v=sys.version_info
if "2.7" in v:
from Tkinter import *
import tkFileDialog
elif "3.3" in v or "3.4" in v:
from tkinter import *
import tkinter.tkFileDialog'''
root=Tk("Text Editor")
text=Text(root)
text.grid()
def saveas():
global text
t = text.get("1.0", "end-1c")
savelocation=tkFileDialog.asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
button=Button(root, text="Save", command=saveas)
button.grid()
def FontHelvetica():
global text
text.config(font="Helvetica")
def FontCourier():
global text
text.config(font="Courier")
font=Menubutton(root, text="Font")
font.grid()
font.menu=Menu(font, tearoff=0)
font["menu"]=font.menu
Helvetica=IntVar()
arial=IntVar()
times=IntVar()
Courier=IntVar()
font.menu.add_checkbutton(label="Courier", variable=Courier,
command=FontCourier)
'''font.menu.add_checkbutton(label="Helvetica", variable=helvetica,
command=FontHelvetica)'''
root.mainloop()
this code work on Py3.8 07.07.2022
1 year ago
This is my code:
import tkinter
def saveas():
global text
t = text.get("1.0", "end-1c")
savelocation = tkinter.filedialog.asksaveasfilename()
file=open(savelocation, "w+")
file.write(t)
file.close()
def FontHelvetica():
global text
text.config(font="Helvetica")
def FontCourier():
global text
text.config(font="Courier")
root = tkinter.Tk()
text = tkinter.Text(root)
text.grid()
button = tkinter.Button(root, text='Save', command=saveas)
button.grid()
font=tkinter.Menubutton(root, text="Font")
font.grid()
font.menu=tkinter.Menu(font, tearoff=0)
font["menu"]=font.menu
helvetica=tkinter.IntVar()
courier=tkinter.IntVar()
font.menu.add_checkbutton(label="Courier", variable=courier,
command=FontCourier)
font.menu.add_checkbutton(label="Helvetica", variable=helvetica,
command=FontHelvetica)
root.mainloop()
4 years ago
This whole thing is broken for python 3. I will say, this is a great learning tool for an intro into this. Here's what I got:
import os
from tkinter import *
import tkinter.filedialog
from tkinter.filedialog import askopenfilename
root=Tk("Text Editor")
text=Text(root)
text.grid()
def saveas():
global text
t = text.get("1.0", "end-1c")
savelocation=tkFileDialog.asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
button=Button(root, text="Save", command=saveas)
button.grid()
def FontHelvetica():
global text
text.config(font="Helvetica")
def FontCourier():
global text
text.config(font="Courier")
font=Menubutton(root, text="Font")
font.grid()
font.menu=Menu(font, tearoff=0)
font["menu"]=font.menu
Helvetica=IntVar()
arial=IntVar()
times=IntVar()
Courier=IntVar()
font.menu.add_checkbutton(label="Courier", variable=Courier,
command=FontCourier)
font.menu.add_checkbutton(label="Helvetica", variable=Helvetica,
command=FontHelvetica)
root.mainloop()
Reply 1 year ago
for some reason when I run this code this error comes up:
Using Python 3.9 :)
Reply 4 years ago
This code works for python 3 with absolutely no problem.
2 years ago
It keeps telling me
Name error: TK is not defined
Reply 2 years ago
Try doing:
import Tkinter as TK
Question 2 years ago
where can i start learning python?
3 years ago
Where is it thé syntax color ??
Question 4 years ago
Great!
But one more thing:
How do you show all the fonts in the system?
Question 5 years ago on Step 2
What does this error mean?? And,how do i overcome it?
Answer 4 years ago
Just remove the brackets. The author of this instructable forgot that "sys.version" is a string, not a callable.
5 years ago
When I run that command in the terminal at the start to make the window, it comes up with this. python: can't open file ' /pathto/texteditor.py': [Errno 2] No such file or directory.
PLS HELP! I am running this on windows 7. I have installed Python 3.6.5 and I'm using the Atom text editor.
Reply 4 years ago
...Really? YOU ACTUALLY ENTERED "/pathto/"? You're supposed to enter the path to your python file!
4 years ago
Great tut. I just wanted to comment that yes I would like very much to see more of these. And for a question, I am interested in offering the collapsibility feature to such a text editor.
4 years ago
That was really helpful. Just one thing. Is there a way to change the fonts just for the text you are going to insert and not the one that already exists?
Question 4 years ago on Introduction
how could I type in vietnamese inside the editor, your instruction for the editor is working very well with Python 2.7. Thanks much for giving us the very good code for the Editor.
Thanks.
4 years ago
There are some bugs, I am in python 3.
The first thing is that line 4,
v=sys.version()
sys.version is an string and not a function that will return a string
also the command to run the program and make the window is for mac only.
I hope this answered all these questions.
If my answers don't make sense I'm sorry I'm an newbie to tkinter
Question 5 years ago on Step 2
Hi Sir/Mam,
I was checking out your code can you tell me how can I add a check box in front of the text box as I am planning to make it as a ToDo List Application.
5 years ago
Can you make a tutorial that teaches how to add syntax highlighting? (For IDE's)