Skip to content

Instantly share code, notes, and snippets.

@thelocalgodd
Created November 27, 2023 13:02
Show Gist options
  • Save thelocalgodd/cfe22348adea6a19b35330a12ffc33e5 to your computer and use it in GitHub Desktop.
Save thelocalgodd/cfe22348adea6a19b35330a12ffc33e5 to your computer and use it in GitHub Desktop.
Python code to build a Text Editor
# Importing the necessary modules
import sys
import tkinter as tk
from tkinter import filedialog
# Checking the Python version and importing the appropriate modules
v = sys.version
if "2.7" in v:
from Tkinter import *
elif "3.3" in v or "3.4" in v:
from tkinter import *
# Creating the root window
root = tk.Tk()
root.title("[Kwaku's NotePad]") #Title Name of the Window
# Creating a text widget
text = tk.Text(root)
text.grid()
# Function to save the contents of the text widget to a file
def saveas():
t = text.get("1.0", "end-1c")
savelocation = filedialog.asksaveasfilename()
with open(savelocation, "w+") as file1:
file1.write(t)
# Creating a button to trigger the saveas function
button = tk.Button(root, text="Save", command=saveas)
button.grid()
# Running the main event loop
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment