import tkinter
import tkinter as tk
import customtkinter
from tkinter import *
from tkinter import filedialog
from tkinter import font
from tkinter import colorchooser
from tkinter import ttk
root = Tk()
root.title('Archie')
# Transparent root
root.wait_visibility(root)
root.wm_attributes("-alpha", 0.9)
frame = customtkinter.CTkFrame(master=root)
frame.pack(pady=10, padx=30, fill="both", expand=True)
sticker = customtkinter.CTkLabel(master=frame, text='Text Editor',font=("Roboto", 14))
sticker.pack(side=tk.TOP, pady=2)
root.iconbitmap('')
root.geometry("680x800")
root.configure()
# Set variable for open file name
global open_status_name
open_status_name = False
global selected
selected = False
# Create New File function
def new_file():
# Delete previous text
my_text.delete("1.0", END)
root.title('New File - TextPad!')
status_bar.config(text="New File ")
global open_status_name
open_status_name = False
# Open Files
def open_file():
my_text.delete("1.0", END)
# Grab Filename
text_file = filedialog.askopenfilename(initialdir= "/home/knightinmirror/Documentos", title="Open File", filetypes=(("Text Files", "*.txt"),
("HTML Files", "*.html"),
("Python Files", "*.py"),
("All Files", "*.*")))
# Check to see if there is a file name
if text_file:
# Make filename global so we can access it later
global open_status_name
open_status_name = text_file
# Update Status bars
name = text_file
status_bar.config(text=f'{name} ')
name = name.replace("/home/knightinmirror/Documentos/", "")
root.title(f'{name} - TextPad!')
# Open the file
text_file = open(text_file, 'r')
stuff = text_file.read()
# Add file to textbox
my_text.insert(END, stuff)
# Close the opened file
text_file.close()
# Save Files
def save_file():
global open_status_name
if open_status_name:
# Save the file
text_file = open(open_status_name, "w")
text_file.write(my_text.get(1.0, END))
# Close the file
text_file.close()
status_bar.config(text=f'saved: {open_status_name}')
else:
save_as_file()
# Save As File
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=" .*", initialdir='/home/knightinmirror/Documentos/',
filetypes=(("Text Files", "*.txt"), ("HTML Files", "*.html"),
("Python Files", "*.py"), ("All Files", "*.*")))
if text_file:
# Define name bar or status bars
name = text_file
status_bar.config(text=f'Saved: {name}')
name = name.replace("/home/knightinmirror/Documentos/", "")
root.title(f'{name} - TextPad!')
# Save the file
text_file = open(text_file, "w")
text_file.write(my_text.get(1.0, END))
# Close the file
text_file.close()
# Cut text
def cut_text(e):
global selected
# Check to see if keyboard shortcut used
if e:
selected = root.clipboard_get()
else:
if my_text.selection_get():
# Grab selected text from text box
selected = my_text.selection_get()
# Delete Selected Text from text box
my_text.delete("sel.first", "sel.last")
# Clear the clipboard the append
root.clipboard_clear()
root.clipboard_append(selected)
# Copy text
def copy_text(e):
global selected
# Check to see if we used keyboard shortcut
if e:
selected = root.clipboard_get()
if my_text.selection_get():
# Grab selected text from text box
selected = my_text.selection_get()
root.clipboard_clear()
root.clipboard_append(selected)
# Paste Text
def paste_text(e):
global selected
# Check to see if keyboard shutcut used
if e:
selected = root.clipboard_get()
else:
if selected:
position = my_text.index(INSERT)
my_text.insert(position, selected)
# Bold Text
def bold_it():
# Create our font
bold_font = font.Font(my_text, my_text.cget("font"))
bold_font.configure(weight="bold")
# Configure a tag
my_text.tag_configure("bold", font=bold_font)
# Define Current tags
current_tags = my_text.tag_names("sel.first")
# If statment to see if tag has been set
if "bold" in current_tags:
my_text.tag_remove("bold", "sel.first", "sel.last")
else:
my_text.tag_add("bold", "sel.first", "sel.last")
# Italics Text
def italics_it():
# Create our italics
italics_font = font.Font(my_text, my_text.cget("font"))
italics_font.configure(slant="italic")
# Configure a tag
my_text.tag_configure("italic", font=italics_font)
# Define Current tags
current_tags = my_text.tag_names("sel.first")
# If statment to see if tag has been set
if "italic" in current_tags:
my_text.tag_remove("italic", "sel.first", "sel.last")
else:
my_text.tag_add("italic", "sel.first", "sel.last")
# Change Selected Text Color
def text_color():
# Pick a color
my_color = colorchooser.askcolor()[1]
if my_color:
# Create our font
color_font = font.Font(my_text, my_text.cget("font"))
# Configure a tag
my_text.tag_configure("colored", font='color_font', foreground=my_color)
# Define Current tags
current_tags = my_text.tag_names("sel.first")
# If statment to see if tag has been set
if "colored" in current_tags:
my_text.tag_remove("colored", "sel.first", "sel.last")
else:
my_text.tag_add("colored", "sel.first", "sel.last")
# Create tables
def table_it():
pass
# Change bg color
def bg_color():
my_color = colorchooser.askcolor()[1]
if my_color:
my_text.config(bg=my_color)
# Change ALL Text Color
def all_text_color():
my_color = colorchooser.askcolor()[1]
if my_color:
my_text.config(fg=my_color)
# Highlight the selected text
def highlight_it():
my_text.tag_add("start", "1.11","1.17")
my_text.tag_config("start", background= "yellow", foreground= "white")
pass
# Print File Function
def print_file():
pass
# Select all Text
def select_all(e):
# Add sel tag to select all text
my_text.tag_add('sel', '1.0', 'end')
# Clear all Text
def clear_all():
my_text.delete(1.0, END)
# Create a toolbar frame
toolbar_frame = Frame(root)
toolbar_frame.pack(fill=X)
toolbar_frame.config(background='white', padx=60)
# Create Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# Create our Scrollbar for the Text Box
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)
# Crear un widget Text
my_text = Text(root)
# Configurar el widget Text
my_text.config(width=99, height=48, font=("Helvetica", 10), fg="snow",
selectforeground="white", undo=True, bg="#000000")
# Background image
img = tkinter.PhotoImage(file="fondo-neon-min2.png")
# Agregar la imagen de fondo al widget
my_text.image_create(index="1.0", image=img)
my_text.pack()
# Configure our Scrollbar
text_scroll.config(command=my_text.yview, background='brown')
# Create Menu
my_menu = Menu(root)
root.config(menu=my_menu, background='brown')
# Add File Menu
my_menu.configure(background='brown', fg='white')
file_menu = Menu(my_menu, tearoff=False)
my_menu.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_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Print", command=print_file)
file_menu.add_command(label="Exit", command=root.quit)
# Add Edit Menu
edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=lambda: cut_text(False), accelerator="\t Ctrl+x")
edit_menu.add_command(label="Copy", command=lambda: copy_text(False), accelerator="\t Ctrl+c")
edit_menu.add_command(label="Paste", command=lambda: paste_text(False), accelerator="\t Ctrl+v")
edit_menu.add_separator()
edit_menu.add_command(label="Undo", command=my_text.edit_undo, accelerator="\t Ctrl+z")
edit_menu.add_command(label="Redo", command=my_text.edit_redo, accelerator="\t Ctrl+Shift+z")
edit_menu.add_separator()
edit_menu.add_command(label="Select All", command=lambda: select_all(True), accelerator="\t Ctrl+E")
edit_menu.add_command(label="Clear", command=clear_all, accelerator="\t Ctrl+y")
# Add Color Menu
color_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Colores", menu=color_menu)
color_menu.add_command(label="Selected Text", command=text_color)
color_menu.add_command(label="ALL Text", command=all_text_color)
color_menu.add_command(label="Background", command=bg_color)
# Add status Bar to Botton of App
status_bar =Label(root, text='Ready ', anchor=E)
status_bar.pack(fill=X, side=BOTTOM, ipady=5)
# Edit Bindings
root.bind('<Control-Key-x>', cut_text)
root.bind('<Control-Key-c>', copy_text)
root.bind('<Control-Key-v>', paste_text)
# Select Binding
root.bind('Control-E', select_all)
root.bind('Control-e', select_all)
fee = "Now what? "
my_label = Label(root, text=fee[: -1], fg='white').pack()
# Create Button
# Bold Button
bold_button = Button(toolbar_frame, text="Bold", command=bold_it, background='brown', fg='white')
bold_button.grid(row=0, column=0, sticky=W)
# Italics Button
italics_button = Button(toolbar_frame, text="Italics", command=italics_it, background='brown', fg='white')
italics_button.grid(row=0, column=1)
# Undo/Redo Buttons
undo_button = Button(toolbar_frame, text="Undo", command=my_text.edit_undo, background='brown', fg='white')
undo_button.grid(row=0, column=2)
redo_button = Button(toolbar_frame, text="Redo", command=my_text.edit_redo, background='brown', fg='white')
redo_button.grid(row=0, column=3)
# Text Color
color_text_button = Button(toolbar_frame, text="Text Color", command=text_color, background='brown', fg='white')
color_text_button.grid(row=0, column=4, padx=5)
# Highlight Button
highlight_button = Button(toolbar_frame, text="Highlight", command=highlight_it, background='brown', fg='white')
highlight_button.grid(row=0, column=5)
# Tables Button
table_button = Button(toolbar_frame, text="Tables", command=table_it, background='brown', fg='white')
table_button.grid(row=0, column=6)
root.mainloop()