Python Tkinter editor

In this Python tutorial, we will learn How to create a Tkinter editor in Python Tkinter and we will also cover different examples related to Tkinter Editor. And we will cover these topics.

  • Python Tkinter editor
  • Python Tkinter text editor
  • Python Tkinter GUI editor
  • Python Tkinter JSON editor
  • Python Tkinter image editor

Python Tkinter editor

In this part of the Python tkinter editor tutorial, we will learn about How to create a Tkinter editor in Python from which we edit our code, text, Picture and give the perfect output to the programmer.

Code:

In the following code, we will import the Tkinter library from which we can create an editor platform where the user can edit their code easily and also get the correct output.

  • wd.geometry(‘500×300’) is used to give the geometry to the window.
  • wd.title(“Pythontpoint”) is used to give the title to the window.
  • texteditor = text_edit.get(0.3,END) is used where we can edit the text or code.
from tkinter import *
import tkinter

wd = Tk()

wd.geometry('500x300')
wd.title("Pythontpoint")

def calling():
    texteditor = text_edit.get(0.3,END)
    print(texteditor)

text_edit = Text(wd, width=56, height=25)

text_edit.pack()

wd.mainloop()

Output:

After running the above code we get the following output in which we can see that the text editor window is shown.

python tkinter editor

Read: Python Tkinter Animation

Python Tkinter text editor

In this part of the Python Tkinter editor tutorial, we will learn how to edit the text in the Python Tkinter text editor in python.

Before moving forward we should have some piece of knowledge about the text editor. A text editor is a platform where we can edit the text and after editing, we can also save the text.

Code:

In the following code, we will import the Tkinter module from tkinter import *, import tkinter from which we can create a text editor and edit the text in it.

  • wd.title(“Pythontpoint”) is used to give the title to the window.
  • text_editor = text_edit.get(0.3,END) is used to edit the text.
  • text_button = Button(wd,text=”Show Text”, command= calling) is used to insert the button on the screen for editing the text.
from tkinter import *
import tkinter

wd = Tk()

wd.geometry('500x300')
wd.title("Pythontpoint")

def calling():
    text_editor = text_edit.get(0.3,END)
    print(text_editor)


text_edit = Text(wd, width=56, height=25)

text_edit.pack()

text_button = Button(wd,text="Show Text", command= calling)
text_button.pack(pady =10)

wd.mainloop()

Output:

After running the above code we get the following output in which we can see that the text is edited on the screen.

Python tkinter text editor
Python Tkinter text editor

Python Tkinter GUI editor

In this part of the python tkinter editor tutorial, we will learn about how to how to edit the code on GUI editor in Python Tkinter.

  • GUI stands for Graphical User Interface where the user can interact with the devices with the help of graphical symbols.
  • Here the user can write the code and want to edit their code in the GUI editor and also want to prepare content over a text box.

Code:

In the following code, we will import the tkinter module from which we can edit our text, code, and image in the editor.

  • wd.title(‘Pythontpoint’) is used to give the title to the window.
  • editor.delete(‘1.5’, END) is used to delete the text from the editor.
  • editor.insert(‘1.5’, code) is used to insert the code in the editor.
  • editor.config(bg=’white’, fg=’red’, insertbackground=’black’) is used to config the editor.
from tkinter import *
from tkinter.filedialog import askopenfilename, asksaveasfilename
import subprocess
import tkinter

wd = Tk()
wd.title('Pythontpoint')

Cpath = ''


     

def openthisfile():
    path = askopenfilename(filetypes=[('Python Files','*.py')])
    with open(path, 'r') as file:
        code = file.read()
        editor.delete('1.5', END)
        editor.insert('1.5', code)
        global Cpath
        Cpath = path

def savethisfileAs():
    global gpath
    if Cpath =='':
        path = asksaveasfilename(filetypes=[('Python Files','*.py')])
    else:
        path = Cpath    
    with open(path, 'w') as file:
        code = editor.get('1.5', END)
        file.write(code)

editor = Text()
editor.config(bg='white', fg='red', insertbackground='black')
editor.pack()

result = Text(height=7)
result.config(bg='black', fg='blue')
result.pack()
 
menu_option = Menu(wd)

file_option = Menu(menu_option, tearoff=0)
file_option.add_command(label='Open', command = openthisfile)
file_option.add_command(label='Save', command = savethisfileAs)
file_option.add_command(label='SaveAs', command = savethisfileAs)
file_option.add_command(label='Exit', command = exit)
menu_option.add_cascade(label='File', menu = file_option)

wd.config(menu=menu_option)
wd.mainloop()

Output:

After running the above code we get the following output in which we can see that the GUI editor is open on the screen where the user can edit their code.

Python tkinter GUI editor
Python tkinter GUI editor

After editing the code we want to run the code before running the code we want to just save the code. After saving now the code is ready to run.

Python tkinter GUI editor save file
Python Tkinter GUI editor save file

After running the code we see that the text is printed on the screen.

Python tkinter GUI editor output
Python Tkinter GUI editor output

Python Tkinter JSON editor

In this part of the Python Tkinter editor tutorial, we will learn about how to work with JSON editors in Python.

Before moving forward we should have some piece of knowledge about JSON. JavaScript Object Notation is a full form of JSON in this data and objects are stored using Key-values.

In JSON editor the user can add their code or text the editor the JSON editor stores the data in the form of key-value and run the code.

Code:

In the following code, we will import the tkinter library from which we can create the JSON editor, and we can also edit our code.

  • wd.geometry(‘500×300’) is used to give the geometry to the window.
  • wd.title(‘Pythontpoint’) is used to give the title to the window.
  • File_position = asksaveasfile(filetypes = File,defaultextension = json,initialfile=’IOTEDU’) is used to save thefile through dialogue box.
from tkinter import *
import json
from tkinter.filedialog import asksaveasfile
import tkinter
 
wd = Tk()
wd.geometry('500x300')
wd.title('Pythontpoint')
 

 
test = 1
def jSONfile(path, fileName, data):
        json.dump(data, path)
 
path = './'
 
 
def check():
    a= int(ID.get())
    b = test * Name.get()
    c = Batch.get()
    print(a)
    print(b)
    print(c)
    data = {}
    data['ID'] = a
    data['Name'] = b
    data['Batch'] = c
    File = [('JSON_File', '*.json')]
    File_Name='IOTEDU'
    File_position = asksaveasfile(filetypes = File,defaultextension = json,initialfile='IOTEDU')
    jSONfile(File_position, File_Name, data)

id = Label(wd, text="ID:")
ID = Entry(wd)
name = Label(wd, text="Name:")
Name = Entry(wd)
batch = Label(wd, text="Batch:")
Batch = Entry(wd)

 
id.grid(row=0, column=0)
ID.grid(row=0,column=1)
name.grid(row=4,column=0)
Name.grid(row=4, column=1)
batch.grid(row=6, column=0)
Batch.grid(row=6, column=1)

submit = Button(wd,text='Submit',command = check).grid(row=8, column=1)
 
wd.mainloop()

Output:

After running the above code we get the following output in which we can see that the information section is seen on the screen.

Python tkinter JSON Editor
Python Tkinter JSON Editor

Here we can fill the entry and after filling the entries click on the submit button after clicking on the button our information is saved.

JSON Python tkinter editor
JSON Python Tkinter editor

After clicking on the submit button the information is shown in this form as shown in the picture.

JSON Python tkinter editor output
JSON Python Tkinter editor output

Python Tkinter image editor

In this part of the Python Tkinter editor tutorial, we will learn how to add or edit images in an image editor.

Here we can edit the image in an image editor like rotation, left to right flip, increase brightness, and adding contrast from which our image looks attractive and beautiful.

Code:

In the following code, we will import the tkinter module from which we can create an image editor in which we can edit our image.

  • wd.title(“Pythontpoint”) is used to give the title on the window.
  • imagepath = filedialog.askopenfilename(initialdir=os.getcwd()) is used to select the image from our device.
  • img.filter(ImageFilter.BoxBlur(m)) is used to blur the image.
  • ImageEnhance.Brightness(img) is used to change the brightness of the image.
  • img.rotate(int(Rotate_combo.get())) is used to rotate the image from left to right or top to bottom.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename,asksaveasfilename
from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps
import os
wd = Tk()
wd.title("Pythontpoint")
wd.geometry("640x640")
def selected():
    global imagepath, img
    imagepath = filedialog.askopenfilename(initialdir=os.getcwd()) 
    img = Image.open(imagepath)
    img.thumbnail((350, 350))
    img1 = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=img1)
    canvas2.img=img1                                                                                                                                                                                                                
def blur(event):
    global imagepath, img1, imgg
    for m in range(0, v1.get()+1):
            img = Image.open(imagepath)
            img.thumbnail((350, 350))
            imgg = img.filter(ImageFilter.BoxBlur(m))
            img1 = ImageTk.PhotoImage(imgg) 
            canvas2.create_image(300, 210, image=img1)
            canvas2.img=img1
def brightness(event):
    global imagepath, img2, img3
    for m in range(0, v2.get()+1):
            img = Image.open(imagepath)
            img.thumbnail((350, 350))
            imgg = ImageEnhance.Brightness(img)
            img2 = imgg.enhance(m)
            img3 = ImageTk.PhotoImage(img2)
            canvas2.create_image(300, 210, image=img3)
            canvas2.img=img3
def contrast(event):
    global imagepath, img4, img5
    for m in range(0, v3.get()+1):
            img = Image.open(imagepath)
            img.thumbnail((350, 350))
            imgg = ImageEnhance.Contrast(img)
            img4 = imgg.enhance(m)
            img5 = ImageTk.PhotoImage(img4)
            canvas2.create_image(300, 210, image=img5)
            canvas2.img=img5
def rotate_image(event):
        global image_path, img6, img7
        img = Image.open(imagepath)
        img.thumbnail((350, 350))
        img6 = img.rotate(int(Rotate_combo.get()))
        img7 = ImageTk.PhotoImage(img6)
        canvas2.create_image(300, 210, image=img7)
        canvas2.img=img7
        
def flipimage(event):
        global imagepath, img8, img9
        img = Image.open(imagepath)
        img.thumbnail((350, 350))
        if Flip_combo.get() == "FLIP LEFT TO RIGHT":
            img8 = img.transpose(Image.FLIP_LEFT_RIGHT)
        elif Flip_combo.get() == "FLIP TOP TO BOTTOM":
            img8 = img.transpose(Image.FLIP_TOP_BOTTOM)
        img9 = ImageTk.PhotoImage(img8)
        canvas2.create_image(300, 210, image=img9)
        canvas2.img=img9   
def imageborder(event):
    global imagepath, img10, img11
    img = Image.open(imagepath)
    img.thumbnail((350, 350))
    img10 = ImageOps.expand(img, border=int(Border_combo.get()), fill=95)
    img11 = ImageTk.PhotoImage(img10)
    canvas2.create_image(300, 210, image=img11)
    canvas2.img=img11    
img1 = None
img3 = None
img5 = None
img7 = None
img9 = None
img11 = None
def save():
    global imagepath, imgg, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, imge11
    #file=None
    ext = imagepath.split(".")[-1]
    file=asksaveasfilename(defaultextension =f".{ext}",filetypes=[("All Files","*.*"),("PNG file","*.png"),("jpg file","*.jpg")])
    if file: 
            if canvas2.img==img1:
                imgg.save(file)
            elif canvas2.img==img3:
                img2.save(file)
            elif canvas2.img==img5:
                img4.save(file)
            elif canvas2.image==img7:
                img6.save(file)
            elif canvas2.img==img9:
                img8.save(file)
            elif canvas2.img==img11:
                img10.save(file)        
blurr = Label(wd, text="Blur:", font=("ariel 17 bold"), width=9, anchor='e')
blurr.place(x=15, y=8)
v1 = IntVar()
scale = ttk.Scale(wd, from_=0, to=10, variable=v1, orient=HORIZONTAL, command=blur) 
scale.place(x=150, y=10)
bright = Label(wd, text="Brightness:", font=("ariel 17 bold"))
bright.place(x=8, y=50)
v2 = IntVar()   
Scale1 = ttk.Scale(wd, from_=0, to=10, variable=v2, orient=HORIZONTAL, command=brightness) 
Scale1.place(x=150, y=55)
contrast = Label(wd, text="Contrast:", font=("ariel 17 bold"))
contrast.place(x=35, y=92)
v3 = IntVar()   
Scale2 = ttk.Scale(wd, from_=0, to=10, variable=v3, orient=HORIZONTAL, command=contrast) 
Scale2.place(x=150, y=100)
Rotate = Label(wd, text="Rotate:", font=("ariel 17 bold"))
Rotate.place(x=370, y=8)
values = [0, 90, 180, 270, 360]
Rotate_combo = ttk.Combobox(wd, values=values, font=('ariel 10 bold'))
Rotate_combo.place(x=460, y=15)
Rotate_combo.bind("<<ComboboxSelected>>", rotate_image)
Flip = Label(wd, text="Flip:", font=("ariel 17 bold"))
Flip.place(x=400, y=50)
values1 = ["FLIP LEFT TO RIGHT", "FLIP TOP TO BOTTOM"]
Flip_combo = ttk.Combobox(wd, values=values1, font=('ariel 10 bold'))
Flip_combo.place(x=460, y=57)
Flip_combo.bind("<<ComboboxSelected>>", flipimage)
border = Label(wd, text="Add border:", font=("ariel 17 bold"))
border.place(x=320, y=92)
values2 = [i for i in range(10, 45, 5)]
Border_combo = ttk.Combobox(wd, values=values2, font=("ariel 10 bold"))
Border_combo.place(x=460, y=99)
Border_combo.bind("<<ComboboxSelected>>", imageborder)
# create canvas to display image
canvas2 = Canvas(wd, width="600", height="420", relief=RIDGE, bd=2)
canvas2.place(x=15, y=150)
# create buttons
button1 = Button(wd, text="Select Image", bg='black', fg='gold', font=('ariel 15 bold'), relief=GROOVE, command=selected)
button1.place(x=100, y=595)
button2 = Button(wd, text="Save", width=12, bg='black', fg='gold', font=('ariel 15 bold'), relief=GROOVE, command=save)
button2.place(x=280, y=595)
button3 = Button(wd, text="Exit", width=12, bg='black', fg='gold', font=('ariel 15 bold'), relief=GROOVE, command=wd.destroy)
button3.place(x=460, y=595)
wd.mainloop()

Output:

After running the above code we get the following output in which we can see that an image editor is shown on the screen.

Python tkinter image editor output
Python Tkinter image editor output

Here we can select the image by clicking on the select image button after choosing the image we can adjust the image accordingly as we want.

Python tkinter image editor
Python Tkinter image editor

So, in this Python tutorial, we discussed Python Tkinter editor and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python Tkinter editor
  • Python Tkinter text editor
  • Python Tkinter GUI editor
  • Python Tkinter JSON editor
  • Python Tkinter image editor

3 thoughts on “Python Tkinter editor”

  1. Do you mind if Ӏ quote a couple of your articles as long as I provide credit and sources back to your wеbsite?
    My blog site is in the exact same area of interest
    aѕ yours and my users would definitely benefit from a lot of the information you рroviɗe heгe.
    Please let me know if this alright with you. Thank yoᥙ!

    Reply
  2. Appreciating the dedication you put into your site and in depth information you offer. It’s good to come across a blog every once in a while that isn’t the same unwanted rehashed material. Excellent read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

    Reply
  3. The subsequent time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my option to read, however I truly thought youd have one thing attention-grabbing to say. All I hear is a bunch of whining about something that you would fix in case you werent too busy searching for attention.

    Reply

Leave a Comment