Skip to content

Commit

Permalink
Merge pull request #13 from Mmabiaa/Content-Branch
Browse files Browse the repository at this point in the history
Uploading Updates
  • Loading branch information
Mmabiaa authored Jan 6, 2025
2 parents 3fd55f3 + a220312 commit 23307ee
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 36 deletions.
77 changes: 77 additions & 0 deletions text_editor/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from tkinter import Menu
from file_operations import *
from text_formatting import *
from insertions import *
from tkinter import messagebox


def create_menu(root):
menu = Menu(root)
root.config(menu=menu)

# File menu
file_menu = Menu(menu, tearoff=False)
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="Exit", command=root.quit)

# Edit menu
edit_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Undo", command=lambda: text_area.event_generate("<<Undo>>"))
edit_menu.add_command(label="Redo", command=lambda: text_area.event_generate("<<Redo>>"))
edit_menu.add_separator()
edit_menu.add_command(label="Cut", command=lambda: text_area.event_generate("<<Cut>>"))
edit_menu.add_command(label="Copy", command=lambda: text_area.event_generate("<<Copy>>"))
edit_menu.add_command(label="Paste", command=lambda: text_area.event_generate("<<Paste>>"))
edit_menu.add_command(label="Select All", command=lambda: text_area.event_generate("<<SelectAll>>"))
edit_menu.add_separator()

# Format menu
format_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Format", menu=format_menu)
format_menu.add_command(label="Font", command=choose_font)
format_menu.add_command(label="Increase Font Size", command=increase_font_size)
format_menu.add_command(label="Decrease Font Size", command=decrease_font_size)
format_menu.add_command(label="Color", command=choose_color)
format_menu.add_separator()
format_menu.add_command(label="Bold", command=apply_bold)
format_menu.add_command(label="Italic", command=apply_italic)
format_menu.add_command(label="Underline", command=apply_underline)
format_menu.add_command(label="Strikethrough", command=apply_strikethrough)

# Theme menu
theme_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Theme", menu=theme_menu)
theme_menu.add_command(label="Light", command=lambda: change_theme("light"))
theme_menu.add_command(label="Dark", command=lambda: change_theme("dark"))
theme_menu.add_command(label="Gray", command=lambda: change_theme("gray"))
theme_menu.add_command(label="Green", command=lambda: change_theme("green"))
theme_menu.add_command(label="Blue", command=lambda: change_theme("blue"))
theme_menu.add_command(label="Purple", command=lambda: change_theme("purple"))
theme_menu.add_command(label="Red", command=lambda: change_theme("red"))
theme_menu.add_command(label="Orange", command=lambda: change_theme("orange"))
theme_menu.add_command(label="Yellow", command=lambda: change_theme("yellow"))
theme_menu.add_command(label="Brown", command=lambda: change_theme("brown"))
theme_menu.add_command(label="Pink", command=lambda: change_theme("pink"))
theme_menu.add_command(label="Cyan", command=lambda: change_theme("cyan"))
theme_menu.add_command(label="Custom", command=lambda: change_theme("custom"))


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

# Insert menu
insert_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Insert", menu=insert_menu)
insert_menu.add_command(label="Image", command=insert_image)
insert_menu.add_command(label="Video", command=insert_video)

def about():
messagebox.showinfo("About", "Mmabiaa Text Editor\nVersion 1.0\n\n Created by Boateng Agyenim Prince\n\n A simple text editor built using python and tkinter")
45 changes: 25 additions & 20 deletions text_editor/file_operations.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
from tkinter import filedialog, messagebox
from tkinter import filedialog, messagebox, END
import os
from text_formatting import *

filename = None

def new_file(text_area):
def new_file(root):
#new_file : clears the text area and resets the filename
global filename
text_area.delete(1.0, 'end')
text_area.delete(1.0, END)
filename = None
root.title("Mmabia Text Pad- Untitled File")

def open_file(text_area):
def open_file(root):
#open_file : opens a file dialog,reads the selected file and displays it contents in the text area
global filename
filename = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("All Files", "."), ("Text Documents", "*.txt")])

if filename:
with open(filename, 'r') as file:
text_area.delete(1.0, 'end')
text_area.delete(1.0, END)
text_area.insert(1.0, file.read())
root.title(f"Mmabia Textpad - {os.path.basename(filename)}")

def save_file(text_area):

def save_file(root):
#save_file : saves the current file
global filename

if filename:
try:
with open(filename, 'w') as file:
file.write(text_area.get(1.0, 'end'))
file.write(text_area.get(1.0, END))
root.title(f"Mmabia Textpad - {os.path.basename(filename)}")
except Exception as e:
messagebox.showerror("Error", str(e))

def save_as_file(text_area):
else:
save_as_file()


def save_as_file(root):
#save_as_file : opens a save dialog and saves the file with a name
global filename

filename = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("All Files", "."), ("Text Documents", "*.txt")])

if filename:
with open(filename, 'w') as file:
file.write(text_area.get(1.0, 'end'))

def create_text_area(root):
from tkinter.scrolledtext import ScrolledText

global text_area
text_area = ScrolledText(root, wrap='word', undo=True)
file.write(text_area.get(1.0, END))
root.title(f"Mmabiaa Textpad - {os.path.basename(filename)}")


19 changes: 19 additions & 0 deletions text_editor/insertions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from tkinter import filedialog, messagebox, PhotoImage, END
from text_formatting import*



def insert_image():
filepath = filedialog.askopenfilename(filetypes=[("Image Files", ".png;.jpg;.jpeg;.gif")])
if filepath:
try:
image = PhotoImage(file=filepath)
text_area.image_create(END, image=image)
text_area.image = image # Keep a reference to avoid garbage collection
except Exception as e:
messagebox.showerror("Error", str(e))

def insert_video():
filepath = filedialog.askopenfilename(filetypes=[("Video Files", ".mp4;.avi;*.mov")])
if filepath:
messagebox.showinfo("Info", "Video inserted. (This is a placeholder implementation.)")
3 changes: 1 addition & 2 deletions text_editor/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from tkinter import Tk
from commands import create_menu
from file_operations import create_text_area
from text_formatting import create_text_area
from text_formatting import *
# Initialize the main Tkinter window
root = Tk()
root.title("Mmabiaa Textpad")
Expand Down
Empty file removed text_editor/menu.py
Empty file.
95 changes: 81 additions & 14 deletions text_editor/text_formatting.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
from tkinter import simpledialog, font, TclError
from tkinter import simpledialog, font, TclError, colorchooser
from tkinter.scrolledtext import ScrolledText


current_font_family = "Times New Roman"
current_font_size = 18


def change_theme(theme):
if theme == "light":
text_area.configure(bg="white", fg="black")
elif theme == "dark":
text_area.configure(bg="black", fg="white")
elif theme == "gray":
text_area.configure(bg="grey", fg="white")
elif theme == "green":
text_area.configure(bg="green", fg="black")
elif theme == "blue":
text_area.configure(bg="blue", fg="white")
elif theme == "purple":
text_area.configure(bg="purple", fg="white")
elif theme == "orange":
text_area.configure(bg="orange", fg="black")
elif theme == "yellow":
text_area.configure(bg="yellow", fg="black")
elif theme == "pink":
text_area.configure(bg="pink", fg="black")
elif theme == "brown":
text_area.configure(bg="brown", fg="white")
elif theme == "cyan":
text_area.configure(bg="cyan", fg="black")
elif theme == "magenta":
text_area.configure(bg="magenta", fg="white")
elif theme == "custom":
text_area.configure(bg="aqua", fg="white")



# Function to choose and set the text color
def choose_color():
#A Function that allows users to choose font color
color = colorchooser.askcolor()[1]
if color:
text_area.configure(fg=color)


def create_text_area(root):
global text_area
text_area = ScrolledText(root, wrap='word', undo=True, font=(current_font_family, current_font_size))
text_area.pack(fill='both', expand=1)
text_area.focus_set()

def choose_font(text_area):
"""Open a dialog to choose a font family and size."""
def choose_font():
#choose_font : opens a font dialog and sets the font
global current_font_family, current_font_size

font_family = simpledialog.askstring("Font", "Enter font family:")
font_size = simpledialog.askinteger("Font", "Enter font size:")

if font_family and font_size:
current_font_family = font_family
current_font_size = font_size
text_area.configure(font=(current_font_family, current_font_size))


def increase_font_size(text_area):
"""Increase the font size by 5."""
global current_font_size
Expand All @@ -35,8 +74,7 @@ def decrease_font_size(text_area):
current_font_size -= 5
text_area.configure(font=(current_font_family, current_font_size))

def apply_bold(text_area):
"""Toggle bold formatting on the selected text."""
def apply_bold():
try:
current_tags = text_area.tag_names("sel.first")
if "bold" in current_tags:
Expand All @@ -49,8 +87,7 @@ def apply_bold(text_area):
except TclError:
pass

def apply_italic(text_area):
"""Toggle italic formatting on the selected text."""
def apply_italic():
try:
current_tags = text_area.tag_names("sel.first")
if "italic" in current_tags:
Expand All @@ -63,8 +100,8 @@ def apply_italic(text_area):
except TclError:
pass

def apply_underline(text_area):
"""Toggle underline formatting on the selected text."""

def apply_underline():
try:
current_tags = text_area.tag_names("sel.first")
if "underline" in current_tags:
Expand All @@ -77,16 +114,46 @@ def apply_underline(text_area):
except TclError:
pass

def apply_strikethrough(text_area):
"""Toggle strikethrough formatting on the selected text."""
def apply_strikethrough():
try:
current_tags = text_area.tag_names("sel.first")
if "strikethrough" in current_tags:
text_area.tag_remove("strikethrough", "sel.first", "sel.last")
else:
text_area.tag_add("strikethrough", "sel.first", "sel.last")
strikethrough_font = font.Font(text_area, text_area.cget("font"))
strikethrough_font.configure(overstrike=True)
strikethrough_font.configure(slant="italic")
text_area.tag_configure("strikethrough", font=strikethrough_font)
except TclError:
pass




def change_theme(theme):
if theme == "light":
text_area.configure(bg="white", fg="black")
elif theme == "dark":
text_area.configure(bg="black", fg="white")
elif theme == "gray":
text_area.configure(bg="grey", fg="white")
elif theme == "green":
text_area.configure(bg="green", fg="black")
elif theme == "blue":
text_area.configure(bg="blue", fg="white")
elif theme == "purple":
text_area.configure(bg="purple", fg="white")
elif theme == "orange":
text_area.configure(bg="orange", fg="black")
elif theme == "yellow":
text_area.configure(bg="yellow", fg="black")
elif theme == "pink":
text_area.configure(bg="pink", fg="black")
elif theme == "brown":
text_area.configure(bg="brown", fg="white")
elif theme == "cyan":
text_area.configure(bg="cyan", fg="black")
elif theme == "magenta":
text_area.configure(bg="magenta", fg="white")
elif theme == "custom":
text_area.configure(bg="aqua", fg="white")
Empty file removed text_editor/theme.py
Empty file.
36 changes: 36 additions & 0 deletions text_editor/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging
import os

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def log_info(message):
"""Log an informational message."""
logging.info(message)

def log_error(message):
"""Log an error message."""
logging.error(message)

def get_file_extension(filename):
"""Return the file extension for a given filename."""
return os.path.splitext(filename)[1]

def is_valid_text_file(filename):
"""Check if the given filename has a valid text file extension."""
valid_extensions = ['.txt', '.md', '.rtf']
return get_file_extension(filename) in valid_extensions

def show_message(title, message):
"""Display a message box with the given title and message."""
from tkinter import messagebox
messagebox.showinfo(title, message)

def confirm_exit():
"""Confirm if the user wants to exit the application."""
from tkinter import messagebox
return messagebox.askokcancel("Quit", "Do you really want to quit?")

def clear_text_area(text_area):
"""Clear the content of the text area."""
text_area.delete(1.0, 'end')

0 comments on commit 23307ee

Please sign in to comment.