GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Create a Text Editor in Python

Gk Scientist December 25, 2022 No Comments
Tweet WhatsApp Telegram

Create a Text Editor in Python:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) Open the PyCharm code editor and create a new project in it. We named the project text_editor.py. To build a Text Editor in Python we need to install the package. For this open the terminal in PyCharm and install one package i.e. tkinter. You install this package by simply typing in the terminal following command i.e. pip install tk and then press enter as shown below.

pip install tk for text editor in python - Create a Text Editor in Python

Note: If the package shows an error while using in the code, you can simply install the package by clicking on the red bulb which appears below the error and you can directly install the package from here.

(2) Now write the following code for creating a Text Editor in Python as shown below.

Text Editor in Python Code 1 - Create a Text Editor in Python
Text Editor in Python Code 2 - Create a Text Editor in Python
Text Editor in Python Code 3 - Create a Text Editor in Python
Text Editor in Python Code 4 - Create a Text Editor in Python
Text Editor in Python Code 5 - Create a Text Editor in Python
Text Editor in Python Code 6 - Create a Text Editor in Python

Python Text Editor Source Code:

import os
from tkinter import *
from tkinter import filedialog, colorchooser, font
from tkinter.messagebox import *
from tkinter.filedialog import *

def change_color():
    color = colorchooser.askcolor(title="pick a color...or else")
    text_area.config(fg=color[1])

def change_font(*args):
    text_area.config(font=(font_name.get(), size_box.get()))

def new_file():
    window.title("Untitled")
    text_area.delete(1.0, END)

def open_file():
    file = askopenfilename(defaultextension=".txt",
                           file=[("All Files", "*.*"),
                                ("Text Documents", "*.txt")])

    if file is None:
        return

    else:
        try:
            window.title(os.path.basename(file))
            text_area.delete(1.0, END)

            file = open(file, "r")

            text_area.insert(1.0, file.read())

        except Exception:
            print("couldn't read file")

        finally:
            file.close()

def save_file():
    file = filedialog.asksaveasfilename(initialfile='unititled.txt',
                                        defaultextension=".txt",
                                        filetypes=[("All Files", "*.*"),
                                                   ("Text Documents", "*.txt")])

    if file is None:
        return

    else:
        try:
            window.title(os.path.basename(file))
            file = open(file, "w")

            file.write(text_area.get(1.0, END))

        except Exception:
            print("couldn't save file")

        finally:
            file.close()

def cut():
    text_area.event_generate("<<Cut>>")

def copy():
    text_area.event_generate("<<Copy>>")

def paste():
    text_area.event_generate("<<Paste>>")


def about():
    showinfo("About this program", "This is a basic text editor program written by YOU....")

def quit():
    window.destroy()

window = Tk()
window.title("Simple Text Editor")
file = None

window_width = 600
window_height = 600
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

x = int((screen_width / 2) - (window_width / 2))
y = int((screen_height / 2) - (window_height / 2))

window.geometry("{}x{}+{}+{}".format(window_width, window_height, x, y))

font_name = StringVar(window)
font_name.set("Open Sans")

font_size = StringVar(window)
font_size.set("20")

text_area = Text(window, font=(font_name.get(), font_size.get()))

scroll_bar = Scrollbar(text_area)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
text_area.grid(sticky=N + E + S + W)
scroll_bar.pack(side=RIGHT, fill=Y)
text_area.config(yscrollcommand=scroll_bar.set)

frame = Frame(window)
frame.grid()

color_button = Button(frame, text="color", command=change_color)
color_button.grid(row=0, column=0)

font_box = OptionMenu(frame, font_name, *font.families(), command=change_font)
font_box.grid(row=0, column=1)

size_box = Spinbox(frame, from_=1, to=100, textvariable=font_size, command=change_font)
size_box.grid(row=0, column=2)

menu_bar = Menu(window)
window.config(menu=menu_bar)

file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit)

edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=cut)
edit_menu.add_command(label="Copy", command=copy)
edit_menu.add_command(label="Paste", command=paste)

help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=about)

window.mainloop()

(3) After running the code, the output window will pop us as shown below.

Text Editor in Python Output - Create a Text Editor in Python

Tutorial MySQL
Tutorial Python
Utility Software
Application Software
Database Models or Data Models
Database Management System (DBMS)
Procedure-Oriented Programming
Object-Oriented Programming (OOP)
Python (programming language)– Wikipedia
Prev Article
Next Article

Related Articles

How to Install Anaconda Python for Machine Learning [Jupyter Notebook]
Install Anaconda Python for Machine Learning: (1) This blog will …
Gk Scientist January 30, 2023 Computer

How to Install Anaconda Python for Machine Learning [Jupyter Notebook]

Implement Queue in Python
Queue in Python: We can use the PyCharm code editor …
Gk Scientist November 29, 2022 Computer

Implement Queue in Python

Java Array
Java Array: We can use the Eclipse IDE for this …
Gk Scientist January 2, 2023 Computer

Java Array

Structured Programming- Advantages and Disadvantages
Structured Programming: In structured programming design, programs are broken into …
Gk Scientist August 9, 2022 Computer

Structured Programming- Advantages and Disadvantages

World Wide Web (WWW)
World Wide Web: From the late 1960s to the early …
Gk Scientist September 16, 2022 Computer

World Wide Web (WWW)

Java StopWatch
Java StopWatch: We can use the Eclipse IDE for this …
Gk Scientist January 18, 2023 Computer

Java StopWatch

Java while Loop
Java while Loop: We can use the Eclipse IDE for …
Gk Scientist January 2, 2023 Computer

Java while Loop

Internet and Its Uses
Internet: Nowadays, the Internet has become an integral part of …
Gk Scientist September 14, 2022 Computer

Internet and Its Uses

Leave a Reply Cancel Reply

Search

  • Popular
  • Recent




GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (33) Biology Questions (88) Chemistry (57) Computer (215) Current Affairs (4) Current Content (0) Economy (15) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (114) Physics (89) Political Science (132) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...