Introduction: Create a Simple Python Text Editor!

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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()

Comments

mcovic (author)2017-10-28

Can you make a tutorial that teaches how to add syntax highlighting? (For IDE's)

AdityaG110 (author)2017-10-26

wow interesting

CarsenE (author)2016-05-23

Why does mine say "error in line 4" "v = sys.version()" "'str' Object not callable"

krowley1 (author)CarsenE2016-12-15

Because sys.version is a property and not a method. It should read:

v=sys.version

without the (). Possibly the author was thinking of platform.python_version() which is a method that prints the same information (if you include platform).

This is a nice little tutorial, but that error should be corrected...

krowley1 (author)krowley12016-12-15

Also, font.menu.add_checkbutton for Helvetica has the variable name in lower case (variable=helvetica), but this gives an error as the font name was earlier defined and should start with a cap:

variable=Helvetica

or, you could change the definition to: helvetica=IntVar()

fix those two errors (sys.version and Helvetica) and it should run.

PumpkinSmasher (author)krowley12017-01-04

thanks, sorry been completely inactive for a year and this was written when I was still learning myself. Will fix now.

DIY Hacks and How Tos (author)2016-01-10

Great python tutorial.

About This Instructable

16,646views

14favorites

More by PumpkinSmasher:Create a simple Python Text Editor!
Add instructable to: